2 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 * Some functions are based on older functions written by Mathieu Desnoyers.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-LIVE-SRC-METADATA"
34 #include <babeltrace/compat/uuid-internal.h>
35 #include <babeltrace/compat/memstream-internal.h>
36 #include <babeltrace/babeltrace.h>
39 #include "../common/metadata/decoder.h"
41 #define TSDL_MAGIC 0x75d11d57
43 struct packet_header
{
47 uint32_t content_size
;
49 uint8_t compression_scheme
;
50 uint8_t encryption_scheme
;
51 uint8_t checksum_scheme
;
54 } __attribute__((__packed__
));
57 enum bt_lttng_live_iterator_status
lttng_live_update_clock_map(
58 struct lttng_live_trace
*trace
)
60 enum bt_lttng_live_iterator_status status
=
61 BT_LTTNG_LIVE_ITERATOR_STATUS_OK
;
65 BT_PUT(trace
->cc_prio_map
);
66 trace
->cc_prio_map
= bt_clock_class_priority_map_create();
67 if (!trace
->cc_prio_map
) {
71 count
= bt_trace_get_clock_class_count(trace
->trace
);
72 BT_ASSERT(count
>= 0);
74 for (i
= 0; i
< count
; i
++) {
75 struct bt_clock_class
*clock_class
=
76 bt_trace_get_clock_class_by_index(trace
->trace
, i
);
78 BT_ASSERT(clock_class
);
79 ret
= bt_clock_class_priority_map_add_clock_class(
80 trace
->cc_prio_map
, clock_class
, 0);
90 status
= BT_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
96 enum bt_lttng_live_iterator_status
lttng_live_metadata_update(
97 struct lttng_live_trace
*trace
)
99 struct lttng_live_session
*session
= trace
->session
;
100 struct lttng_live_metadata
*metadata
= trace
->metadata
;
102 size_t size
, len_read
= 0;
103 char *metadata_buf
= NULL
;
105 enum ctf_metadata_decoder_status decoder_status
;
106 enum bt_lttng_live_iterator_status status
=
107 BT_LTTNG_LIVE_ITERATOR_STATUS_OK
;
109 /* No metadata stream yet. */
111 if (session
->new_streams_needed
) {
112 status
= BT_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
114 session
->new_streams_needed
= true;
115 status
= BT_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE
;
120 if (!metadata
->trace
) {
121 trace
->new_metadata_needed
= false;
124 if (!trace
->new_metadata_needed
) {
128 /* Open for writing */
129 fp
= bt_open_memstream(&metadata_buf
, &size
);
131 BT_LOGE("Metadata open_memstream: %s", strerror(errno
));
135 /* Grab all available metadata. */
138 * get_one_metadata_packet returns the number of bytes
139 * received, 0 when we have received everything, a
140 * negative value on error.
142 ret
= lttng_live_get_one_metadata_packet(trace
, fp
);
149 * Consider metadata closed as soon as we get an error reading
150 * it (e.g. cannot be found).
153 if (!metadata
->closed
) {
154 metadata
->closed
= true;
156 * Release our reference on the trace as soon as
157 * we know the metadata stream is not available
158 * anymore. This won't necessarily teardown the
159 * metadata objects immediately, but only when
160 * the data streams are done.
162 lttng_live_unref_trace(metadata
->trace
);
163 metadata
->trace
= NULL
;
165 if (errno
== EINTR
) {
166 if (lttng_live_is_canceled(session
->lttng_live
)) {
167 status
= BT_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
173 if (bt_close_memstream(&metadata_buf
, &size
, fp
)) {
174 BT_LOGE("bt_close_memstream: %s", strerror(errno
));
181 status
= BT_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
184 trace
->new_metadata_needed
= false;
188 fp
= bt_fmemopen(metadata_buf
, len_read
, "rb");
190 BT_LOGE("Cannot memory-open metadata buffer: %s",
195 decoder_status
= ctf_metadata_decoder_decode(metadata
->decoder
, fp
);
196 switch (decoder_status
) {
197 case CTF_METADATA_DECODER_STATUS_OK
:
198 BT_PUT(trace
->trace
);
199 trace
->trace
= ctf_metadata_decoder_get_trace(metadata
->decoder
);
200 trace
->new_metadata_needed
= false;
201 status
= lttng_live_update_clock_map(trace
);
202 if (status
!= BT_LTTNG_LIVE_ITERATOR_STATUS_OK
) {
206 case CTF_METADATA_DECODER_STATUS_INCOMPLETE
:
207 status
= BT_LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
209 case CTF_METADATA_DECODER_STATUS_ERROR
:
210 case CTF_METADATA_DECODER_STATUS_INVAL_VERSION
:
211 case CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR
:
217 status
= BT_LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
222 closeret
= fclose(fp
);
224 BT_LOGE("Error on fclose");
232 int lttng_live_metadata_create_stream(struct lttng_live_session
*session
,
233 uint64_t ctf_trace_id
,
235 const char *trace_name
)
237 struct lttng_live_metadata
*metadata
= NULL
;
238 struct lttng_live_trace
*trace
;
241 metadata
= g_new0(struct lttng_live_metadata
, 1);
245 metadata
->stream_id
= stream_id
;
246 //TODO: add clock offset option
247 match
= strstr(trace_name
, session
->session_name
->str
);
251 metadata
->decoder
= ctf_metadata_decoder_create(NULL
,
253 if (!metadata
->decoder
) {
256 trace
= lttng_live_ref_trace(session
, ctf_trace_id
);
260 metadata
->trace
= trace
;
261 trace
->metadata
= metadata
;
265 ctf_metadata_decoder_destroy(metadata
->decoder
);
271 void lttng_live_metadata_fini(struct lttng_live_trace
*trace
)
273 struct lttng_live_metadata
*metadata
= trace
->metadata
;
278 if (metadata
->text
) {
279 free(metadata
->text
);
281 ctf_metadata_decoder_destroy(metadata
->decoder
);
282 trace
->metadata
= NULL
;
283 if (!metadata
->closed
) {
284 lttng_live_unref_trace(metadata
->trace
);