`ctf` plugin: metadata: use BT_COMP_LOG*() instead of BT_LOG*()
[babeltrace.git] / src / plugins / ctf / lttng-live / metadata.c
1 /*
2 * Copyright 2019 - Francis Deslauriers <francis.deslauriers@efficios.com>
3 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
6 * Some functions are based on older functions written by Mathieu Desnoyers.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #define BT_LOG_OUTPUT_LEVEL log_level
28 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META"
29 #include "logging/log.h"
30
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <stdbool.h>
35 #include <glib.h>
36 #include "compat/memstream.h"
37 #include <babeltrace2/babeltrace.h>
38
39 #include "metadata.h"
40 #include "../common/metadata/decoder.h"
41
42 #define TSDL_MAGIC 0x75d11d57
43
44 struct packet_header {
45 uint32_t magic;
46 uint8_t uuid[16];
47 uint32_t checksum;
48 uint32_t content_size;
49 uint32_t packet_size;
50 uint8_t compression_scheme;
51 uint8_t encryption_scheme;
52 uint8_t checksum_scheme;
53 uint8_t major;
54 uint8_t minor;
55 } __attribute__((__packed__));
56
57
58 static
59 bool stream_classes_all_have_default_clock_class(bt_trace_class *tc,
60 bt_logging_level log_level)
61 {
62 uint64_t i, sc_count;
63 const bt_clock_class *cc = NULL;
64 const bt_stream_class *sc;
65 bool ret = true;
66
67 sc_count = bt_trace_class_get_stream_class_count(tc);
68 for (i = 0; i < sc_count; i++) {
69 sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i);
70
71 BT_ASSERT(sc);
72
73 cc = bt_stream_class_borrow_default_clock_class_const(sc);
74 if (!cc) {
75 ret = false;
76 BT_LOGE("Stream class doesn't have a default clock class: "
77 "sc-id=%" PRIu64 ", sc-name=\"%s\"",
78 bt_stream_class_get_id(sc),
79 bt_stream_class_get_name(sc));
80 goto end;
81 }
82 }
83
84 end:
85 return ret;
86 }
87 /*
88 * Iterate over the stream classes and returns the first clock class
89 * encountered. This is useful to create message iterator inactivity message as
90 * we don't need a particular clock class.
91 */
92 static
93 const bt_clock_class *borrow_any_clock_class(bt_trace_class *tc)
94 {
95 uint64_t i, sc_count;
96 const bt_clock_class *cc = NULL;
97 const bt_stream_class *sc;
98
99 sc_count = bt_trace_class_get_stream_class_count(tc);
100 for (i = 0; i < sc_count; i++) {
101 sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i);
102 BT_ASSERT(sc);
103
104 cc = bt_stream_class_borrow_default_clock_class_const(sc);
105 if (cc) {
106 goto end;
107 }
108 }
109 end:
110 BT_ASSERT(cc);
111 return cc;
112 }
113
114 BT_HIDDEN
115 enum lttng_live_iterator_status lttng_live_metadata_update(
116 struct lttng_live_trace *trace)
117 {
118 struct lttng_live_session *session = trace->session;
119 struct lttng_live_metadata *metadata = trace->metadata;
120 struct lttng_live_component *lttng_live =
121 session->lttng_live_msg_iter->lttng_live_comp;
122 ssize_t ret = 0;
123 size_t size, len_read = 0;
124 char *metadata_buf = NULL;
125 FILE *fp = NULL;
126 enum ctf_metadata_decoder_status decoder_status;
127 enum lttng_live_iterator_status status =
128 LTTNG_LIVE_ITERATOR_STATUS_OK;
129 bt_logging_level log_level = trace->log_level;
130
131 /* No metadata stream yet. */
132 if (!metadata) {
133 if (session->new_streams_needed) {
134 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
135 } else {
136 session->new_streams_needed = true;
137 status = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
138 }
139 goto end;
140 }
141
142 if (!metadata->trace) {
143 trace->new_metadata_needed = false;
144 }
145
146 if (!trace->new_metadata_needed) {
147 goto end;
148 }
149
150 /* Open for writing */
151 fp = bt_open_memstream(&metadata_buf, &size);
152 if (!fp) {
153 BT_LOGE("Metadata open_memstream: %s", strerror(errno));
154 goto error;
155 }
156
157 /* Grab all available metadata. */
158 do {
159 /*
160 * get_one_metadata_packet returns the number of bytes
161 * received, 0 when we have received everything, a
162 * negative value on error.
163 */
164 ret = lttng_live_get_one_metadata_packet(trace, fp);
165 if (ret > 0) {
166 len_read += ret;
167 }
168 } while (ret > 0);
169
170 /*
171 * Consider metadata closed as soon as we get an error reading
172 * it (e.g. cannot be found).
173 */
174 if (ret < 0) {
175 if (!metadata->closed) {
176 metadata->closed = true;
177 /*
178 * Release our reference on the trace as soon as
179 * we know the metadata stream is not available
180 * anymore. This won't necessarily teardown the
181 * metadata objects immediately, but only when
182 * the data streams are done.
183 */
184 metadata->trace = NULL;
185 }
186 if (errno == EINTR) {
187 if (lttng_live_graph_is_canceled(lttng_live)) {
188 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
189 goto end;
190 }
191 }
192 }
193
194 if (bt_close_memstream(&metadata_buf, &size, fp)) {
195 BT_LOGE("bt_close_memstream: %s", strerror(errno));
196 }
197 ret = 0;
198 fp = NULL;
199
200 if (len_read == 0) {
201 if (!trace->trace) {
202 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
203 goto end;
204 }
205 trace->new_metadata_needed = false;
206 goto end;
207 }
208
209 fp = bt_fmemopen(metadata_buf, len_read, "rb");
210 if (!fp) {
211 BT_LOGE("Cannot memory-open metadata buffer: %s",
212 strerror(errno));
213 goto error;
214 }
215
216 /*
217 * The call to ctf_metadata_decoder_decode will append new metadata to
218 * our current trace class.
219 */
220 decoder_status = ctf_metadata_decoder_decode(metadata->decoder, fp);
221 switch (decoder_status) {
222 case CTF_METADATA_DECODER_STATUS_OK:
223 if (!trace->trace_class) {
224 trace->trace_class =
225 ctf_metadata_decoder_get_ir_trace_class(
226 metadata->decoder);
227 trace->trace = bt_trace_create(trace->trace_class);
228 if (!stream_classes_all_have_default_clock_class(
229 trace->trace_class, log_level)) {
230 /* Error logged in function. */
231 goto error;
232 }
233 trace->clock_class =
234 borrow_any_clock_class(trace->trace_class);
235 }
236 trace->new_metadata_needed = false;
237
238 break;
239 case CTF_METADATA_DECODER_STATUS_INCOMPLETE:
240 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
241 break;
242 case CTF_METADATA_DECODER_STATUS_ERROR:
243 case CTF_METADATA_DECODER_STATUS_INVAL_VERSION:
244 case CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR:
245 goto error;
246 }
247
248 goto end;
249 error:
250 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
251 end:
252 if (fp) {
253 int closeret;
254
255 closeret = fclose(fp);
256 if (closeret) {
257 BT_LOGE("Error on fclose");
258 }
259 }
260 free(metadata_buf);
261 return status;
262 }
263
264 BT_HIDDEN
265 int lttng_live_metadata_create_stream(struct lttng_live_session *session,
266 uint64_t ctf_trace_id,
267 uint64_t stream_id,
268 const char *trace_name)
269 {
270 struct lttng_live_component *lttng_live =
271 session->lttng_live_msg_iter->lttng_live_comp;
272 struct lttng_live_metadata *metadata = NULL;
273 struct lttng_live_trace *trace;
274 const char *match;
275 struct ctf_metadata_decoder_config cfg = {
276 .log_level = session->log_level,
277 .self_comp =
278 bt_self_component_source_as_self_component(
279 lttng_live->self_comp),
280 .clock_class_offset_s = 0,
281 .clock_class_offset_ns = 0,
282 };
283
284 metadata = g_new0(struct lttng_live_metadata, 1);
285 if (!metadata) {
286 return -1;
287 }
288 metadata->log_level = session->log_level;
289 metadata->stream_id = stream_id;
290
291 match = strstr(trace_name, session->session_name->str);
292 if (!match) {
293 goto error;
294 }
295
296 metadata->decoder = ctf_metadata_decoder_create(&cfg);
297 if (!metadata->decoder) {
298 goto error;
299 }
300 trace = lttng_live_borrow_trace(session, ctf_trace_id);
301 if (!trace) {
302 goto error;
303 }
304 metadata->trace = trace;
305 trace->metadata = metadata;
306 return 0;
307
308 error:
309 ctf_metadata_decoder_destroy(metadata->decoder);
310 g_free(metadata);
311 return -1;
312 }
313
314 BT_HIDDEN
315 void lttng_live_metadata_fini(struct lttng_live_trace *trace)
316 {
317 struct lttng_live_metadata *metadata = trace->metadata;
318
319 if (!metadata) {
320 return;
321 }
322 ctf_metadata_decoder_destroy(metadata->decoder);
323 trace->metadata = NULL;
324 g_free(metadata);
325 }
This page took 0.036746 seconds and 5 git commands to generate.