Commit | Line | Data |
---|---|---|
7cdc2bab | 1 | /* |
14f28187 | 2 | * Copyright 2019 - Francis Deslauriers <francis.deslauriers@efficios.com> |
7cdc2bab MD |
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 | ||
350ad6c1 | 27 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META" |
020bc26f PP |
28 | #include "logging.h" |
29 | ||
7cdc2bab MD |
30 | #include <stdio.h> |
31 | #include <stdint.h> | |
32 | #include <stdlib.h> | |
33 | #include <stdbool.h> | |
34 | #include <glib.h> | |
578e048b | 35 | #include "compat/memstream.h" |
3fadfbc0 | 36 | #include <babeltrace2/babeltrace.h> |
7cdc2bab | 37 | |
7cdc2bab MD |
38 | #include "metadata.h" |
39 | #include "../common/metadata/decoder.h" | |
40 | ||
41 | #define TSDL_MAGIC 0x75d11d57 | |
42 | ||
43 | struct packet_header { | |
44 | uint32_t magic; | |
45 | uint8_t uuid[16]; | |
46 | uint32_t checksum; | |
47 | uint32_t content_size; | |
48 | uint32_t packet_size; | |
49 | uint8_t compression_scheme; | |
50 | uint8_t encryption_scheme; | |
51 | uint8_t checksum_scheme; | |
52 | uint8_t major; | |
53 | uint8_t minor; | |
54 | } __attribute__((__packed__)); | |
55 | ||
14f28187 | 56 | |
7cdc2bab | 57 | static |
14f28187 | 58 | bool stream_classes_all_have_default_clock_class(bt_trace_class *tc) |
7cdc2bab | 59 | { |
14f28187 FD |
60 | uint64_t i, sc_count; |
61 | const bt_clock_class *cc = NULL; | |
62 | const bt_stream_class *sc; | |
63 | bool ret = true; | |
7cdc2bab | 64 | |
14f28187 FD |
65 | sc_count = bt_trace_class_get_stream_class_count(tc); |
66 | for (i = 0; i < sc_count; i++) { | |
67 | sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i); | |
7cdc2bab | 68 | |
14f28187 | 69 | BT_ASSERT(sc); |
7cdc2bab | 70 | |
14f28187 FD |
71 | cc = bt_stream_class_borrow_default_clock_class_const(sc); |
72 | if (!cc) { | |
73 | ret = false; | |
74 | BT_LOGE("Stream class doesn't have a default clock class: " | |
75 | "sc-id=%" PRIu64 ", sc-name=\"%s\"", | |
76 | bt_stream_class_get_id(sc), | |
77 | bt_stream_class_get_name(sc)); | |
78 | goto end; | |
7cdc2bab MD |
79 | } |
80 | } | |
81 | ||
7cdc2bab | 82 | end: |
14f28187 FD |
83 | return ret; |
84 | } | |
85 | /* | |
86 | * Iterate over the stream classes and returns the first clock class | |
87 | * encountered. This is useful to create message iterator inactivity message as | |
88 | * we don't need a particular clock class. | |
89 | */ | |
90 | static | |
91 | const bt_clock_class *borrow_any_clock_class(bt_trace_class *tc) | |
92 | { | |
93 | uint64_t i, sc_count; | |
94 | const bt_clock_class *cc = NULL; | |
95 | const bt_stream_class *sc; | |
96 | ||
97 | sc_count = bt_trace_class_get_stream_class_count(tc); | |
98 | for (i = 0; i < sc_count; i++) { | |
99 | sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i); | |
100 | BT_ASSERT(sc); | |
101 | ||
102 | cc = bt_stream_class_borrow_default_clock_class_const(sc); | |
103 | if (cc) { | |
104 | goto end; | |
105 | } | |
106 | } | |
107 | end: | |
108 | BT_ASSERT(cc); | |
109 | return cc; | |
7cdc2bab MD |
110 | } |
111 | ||
112 | BT_HIDDEN | |
14f28187 | 113 | enum lttng_live_iterator_status lttng_live_metadata_update( |
7cdc2bab MD |
114 | struct lttng_live_trace *trace) |
115 | { | |
116 | struct lttng_live_session *session = trace->session; | |
7cdc2bab | 117 | struct lttng_live_metadata *metadata = trace->metadata; |
14f28187 FD |
118 | struct lttng_live_component *lttng_live = |
119 | session->lttng_live_msg_iter->lttng_live_comp; | |
7cdc2bab MD |
120 | ssize_t ret = 0; |
121 | size_t size, len_read = 0; | |
122 | char *metadata_buf = NULL; | |
123 | FILE *fp = NULL; | |
124 | enum ctf_metadata_decoder_status decoder_status; | |
14f28187 FD |
125 | enum lttng_live_iterator_status status = |
126 | LTTNG_LIVE_ITERATOR_STATUS_OK; | |
7cdc2bab MD |
127 | |
128 | /* No metadata stream yet. */ | |
129 | if (!metadata) { | |
130 | if (session->new_streams_needed) { | |
14f28187 | 131 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
7cdc2bab MD |
132 | } else { |
133 | session->new_streams_needed = true; | |
14f28187 | 134 | status = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
7cdc2bab MD |
135 | } |
136 | goto end; | |
137 | } | |
138 | ||
6f79a7cf MD |
139 | if (!metadata->trace) { |
140 | trace->new_metadata_needed = false; | |
141 | } | |
142 | ||
7cdc2bab MD |
143 | if (!trace->new_metadata_needed) { |
144 | goto end; | |
145 | } | |
146 | ||
147 | /* Open for writing */ | |
148 | fp = bt_open_memstream(&metadata_buf, &size); | |
149 | if (!fp) { | |
087bc060 | 150 | BT_LOGE("Metadata open_memstream: %s", strerror(errno)); |
7cdc2bab MD |
151 | goto error; |
152 | } | |
153 | ||
154 | /* Grab all available metadata. */ | |
155 | do { | |
156 | /* | |
157 | * get_one_metadata_packet returns the number of bytes | |
158 | * received, 0 when we have received everything, a | |
159 | * negative value on error. | |
160 | */ | |
161 | ret = lttng_live_get_one_metadata_packet(trace, fp); | |
162 | if (ret > 0) { | |
163 | len_read += ret; | |
164 | } | |
165 | } while (ret > 0); | |
166 | ||
167 | /* | |
168 | * Consider metadata closed as soon as we get an error reading | |
169 | * it (e.g. cannot be found). | |
170 | */ | |
171 | if (ret < 0) { | |
172 | if (!metadata->closed) { | |
173 | metadata->closed = true; | |
174 | /* | |
175 | * Release our reference on the trace as soon as | |
176 | * we know the metadata stream is not available | |
177 | * anymore. This won't necessarily teardown the | |
178 | * metadata objects immediately, but only when | |
179 | * the data streams are done. | |
180 | */ | |
6f79a7cf | 181 | metadata->trace = NULL; |
7cdc2bab | 182 | } |
4c66436f | 183 | if (errno == EINTR) { |
42521b69 | 184 | if (lttng_live_graph_is_canceled(lttng_live)) { |
14f28187 | 185 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
4c66436f MD |
186 | goto end; |
187 | } | |
188 | } | |
7cdc2bab MD |
189 | } |
190 | ||
191 | if (bt_close_memstream(&metadata_buf, &size, fp)) { | |
192 | BT_LOGE("bt_close_memstream: %s", strerror(errno)); | |
193 | } | |
194 | ret = 0; | |
195 | fp = NULL; | |
196 | ||
197 | if (len_read == 0) { | |
198 | if (!trace->trace) { | |
14f28187 | 199 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
7cdc2bab MD |
200 | goto end; |
201 | } | |
202 | trace->new_metadata_needed = false; | |
203 | goto end; | |
204 | } | |
205 | ||
7cdc2bab MD |
206 | fp = bt_fmemopen(metadata_buf, len_read, "rb"); |
207 | if (!fp) { | |
087bc060 | 208 | BT_LOGE("Cannot memory-open metadata buffer: %s", |
7cdc2bab MD |
209 | strerror(errno)); |
210 | goto error; | |
211 | } | |
212 | ||
14f28187 FD |
213 | /* |
214 | * The call to ctf_metadata_decoder_decode will append new metadata to | |
215 | * our current trace class. | |
216 | */ | |
7cdc2bab MD |
217 | decoder_status = ctf_metadata_decoder_decode(metadata->decoder, fp); |
218 | switch (decoder_status) { | |
219 | case CTF_METADATA_DECODER_STATUS_OK: | |
14f28187 FD |
220 | if (!trace->trace_class) { |
221 | trace->trace_class = | |
222 | ctf_metadata_decoder_get_ir_trace_class( | |
223 | metadata->decoder); | |
224 | trace->trace = bt_trace_create(trace->trace_class); | |
225 | if (!stream_classes_all_have_default_clock_class( | |
226 | trace->trace_class)) { | |
227 | /* Error logged in function. */ | |
228 | goto error; | |
229 | } | |
230 | trace->clock_class = | |
231 | borrow_any_clock_class(trace->trace_class); | |
7cdc2bab | 232 | } |
14f28187 FD |
233 | trace->new_metadata_needed = false; |
234 | ||
7cdc2bab MD |
235 | break; |
236 | case CTF_METADATA_DECODER_STATUS_INCOMPLETE: | |
14f28187 | 237 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
7cdc2bab MD |
238 | break; |
239 | case CTF_METADATA_DECODER_STATUS_ERROR: | |
240 | case CTF_METADATA_DECODER_STATUS_INVAL_VERSION: | |
241 | case CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR: | |
242 | goto error; | |
243 | } | |
244 | ||
245 | goto end; | |
246 | error: | |
14f28187 | 247 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
7cdc2bab MD |
248 | end: |
249 | if (fp) { | |
250 | int closeret; | |
251 | ||
252 | closeret = fclose(fp); | |
253 | if (closeret) { | |
087bc060 | 254 | BT_LOGE("Error on fclose"); |
7cdc2bab MD |
255 | } |
256 | } | |
6f79a7cf | 257 | free(metadata_buf); |
7cdc2bab MD |
258 | return status; |
259 | } | |
260 | ||
261 | BT_HIDDEN | |
262 | int lttng_live_metadata_create_stream(struct lttng_live_session *session, | |
263 | uint64_t ctf_trace_id, | |
06994c71 MD |
264 | uint64_t stream_id, |
265 | const char *trace_name) | |
7cdc2bab | 266 | { |
14f28187 FD |
267 | struct lttng_live_component *lttng_live = |
268 | session->lttng_live_msg_iter->lttng_live_comp; | |
7cdc2bab MD |
269 | struct lttng_live_metadata *metadata = NULL; |
270 | struct lttng_live_trace *trace; | |
06994c71 | 271 | const char *match; |
7cdc2bab MD |
272 | |
273 | metadata = g_new0(struct lttng_live_metadata, 1); | |
274 | if (!metadata) { | |
275 | return -1; | |
276 | } | |
277 | metadata->stream_id = stream_id; | |
14f28187 | 278 | |
06994c71 MD |
279 | match = strstr(trace_name, session->session_name->str); |
280 | if (!match) { | |
281 | goto error; | |
282 | } | |
14f28187 FD |
283 | |
284 | metadata->decoder = ctf_metadata_decoder_create( | |
285 | lttng_live->self_comp, NULL); | |
7cdc2bab MD |
286 | if (!metadata->decoder) { |
287 | goto error; | |
288 | } | |
14f28187 | 289 | trace = lttng_live_borrow_trace(session, ctf_trace_id); |
7cdc2bab MD |
290 | if (!trace) { |
291 | goto error; | |
292 | } | |
293 | metadata->trace = trace; | |
294 | trace->metadata = metadata; | |
295 | return 0; | |
296 | ||
297 | error: | |
298 | ctf_metadata_decoder_destroy(metadata->decoder); | |
299 | g_free(metadata); | |
300 | return -1; | |
301 | } | |
302 | ||
303 | BT_HIDDEN | |
304 | void lttng_live_metadata_fini(struct lttng_live_trace *trace) | |
305 | { | |
306 | struct lttng_live_metadata *metadata = trace->metadata; | |
307 | ||
308 | if (!metadata) { | |
309 | return; | |
310 | } | |
7cdc2bab MD |
311 | ctf_metadata_decoder_destroy(metadata->decoder); |
312 | trace->metadata = NULL; | |
7cdc2bab MD |
313 | g_free(metadata); |
314 | } |