Commit | Line | Data |
---|---|---|
7cdc2bab MD |
1 | /* |
2 | * Copyright 2016 - Philippe Proulx <pproulx@efficios.com> | |
3 | * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation | |
4 | * | |
5 | * Some functions are based on older functions written by Mathieu Desnoyers. | |
6 | * | |
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: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
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 | |
23 | * SOFTWARE. | |
24 | */ | |
25 | ||
020bc26f PP |
26 | #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-LIVE-SRC-METADATA" |
27 | #include "logging.h" | |
28 | ||
7cdc2bab MD |
29 | #include <stdio.h> |
30 | #include <stdint.h> | |
31 | #include <stdlib.h> | |
32 | #include <stdbool.h> | |
33 | #include <glib.h> | |
34 | #include <babeltrace/compat/uuid-internal.h> | |
35 | #include <babeltrace/compat/memstream-internal.h> | |
4c66436f | 36 | #include <babeltrace/graph/graph.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 | ||
56 | static | |
57 | enum bt_ctf_lttng_live_iterator_status lttng_live_update_clock_map( | |
58 | struct lttng_live_trace *trace) | |
59 | { | |
60 | enum bt_ctf_lttng_live_iterator_status status = | |
61 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK; | |
62 | size_t i; | |
63 | int count, ret; | |
64 | ||
65 | BT_PUT(trace->cc_prio_map); | |
66 | trace->cc_prio_map = bt_clock_class_priority_map_create(); | |
67 | if (!trace->cc_prio_map) { | |
68 | goto error; | |
69 | } | |
70 | ||
71 | count = bt_ctf_trace_get_clock_class_count(trace->trace); | |
72 | assert(count >= 0); | |
73 | ||
74 | for (i = 0; i < count; i++) { | |
75 | struct bt_ctf_clock_class *clock_class = | |
76 | bt_ctf_trace_get_clock_class_by_index(trace->trace, i); | |
77 | ||
78 | assert(clock_class); | |
79 | ret = bt_clock_class_priority_map_add_clock_class( | |
80 | trace->cc_prio_map, clock_class, 0); | |
81 | BT_PUT(clock_class); | |
82 | ||
83 | if (ret) { | |
84 | goto error; | |
85 | } | |
86 | } | |
87 | ||
88 | goto end; | |
89 | error: | |
90 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
91 | end: | |
92 | return status; | |
93 | } | |
94 | ||
95 | BT_HIDDEN | |
96 | enum bt_ctf_lttng_live_iterator_status lttng_live_metadata_update( | |
97 | struct lttng_live_trace *trace) | |
98 | { | |
99 | struct lttng_live_session *session = trace->session; | |
7cdc2bab MD |
100 | struct lttng_live_metadata *metadata = trace->metadata; |
101 | ssize_t ret = 0; | |
102 | size_t size, len_read = 0; | |
103 | char *metadata_buf = NULL; | |
104 | FILE *fp = NULL; | |
105 | enum ctf_metadata_decoder_status decoder_status; | |
106 | enum bt_ctf_lttng_live_iterator_status status = | |
107 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK; | |
108 | ||
109 | /* No metadata stream yet. */ | |
110 | if (!metadata) { | |
111 | if (session->new_streams_needed) { | |
112 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
113 | } else { | |
114 | session->new_streams_needed = true; | |
115 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
116 | } | |
117 | goto end; | |
118 | } | |
119 | ||
120 | if (!trace->new_metadata_needed) { | |
121 | goto end; | |
122 | } | |
123 | ||
124 | /* Open for writing */ | |
125 | fp = bt_open_memstream(&metadata_buf, &size); | |
126 | if (!fp) { | |
087bc060 | 127 | BT_LOGE("Metadata open_memstream: %s", strerror(errno)); |
7cdc2bab MD |
128 | goto error; |
129 | } | |
130 | ||
131 | /* Grab all available metadata. */ | |
132 | do { | |
133 | /* | |
134 | * get_one_metadata_packet returns the number of bytes | |
135 | * received, 0 when we have received everything, a | |
136 | * negative value on error. | |
137 | */ | |
138 | ret = lttng_live_get_one_metadata_packet(trace, fp); | |
139 | if (ret > 0) { | |
140 | len_read += ret; | |
141 | } | |
142 | } while (ret > 0); | |
143 | ||
144 | /* | |
145 | * Consider metadata closed as soon as we get an error reading | |
146 | * it (e.g. cannot be found). | |
147 | */ | |
148 | if (ret < 0) { | |
149 | if (!metadata->closed) { | |
150 | metadata->closed = true; | |
151 | /* | |
152 | * Release our reference on the trace as soon as | |
153 | * we know the metadata stream is not available | |
154 | * anymore. This won't necessarily teardown the | |
155 | * metadata objects immediately, but only when | |
156 | * the data streams are done. | |
157 | */ | |
158 | lttng_live_unref_trace(metadata->trace); | |
159 | } | |
4c66436f MD |
160 | if (errno == EINTR) { |
161 | if (bt_graph_is_canceled(session->lttng_live->graph)) { | |
162 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
163 | goto end; | |
164 | } | |
165 | } | |
7cdc2bab MD |
166 | } |
167 | ||
168 | if (bt_close_memstream(&metadata_buf, &size, fp)) { | |
169 | BT_LOGE("bt_close_memstream: %s", strerror(errno)); | |
170 | } | |
171 | ret = 0; | |
172 | fp = NULL; | |
173 | ||
174 | if (len_read == 0) { | |
175 | if (!trace->trace) { | |
176 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
177 | goto end; | |
178 | } | |
179 | trace->new_metadata_needed = false; | |
180 | goto end; | |
181 | } | |
182 | ||
183 | if (babeltrace_debug) { | |
184 | // yydebug = 1; | |
185 | } | |
186 | ||
187 | fp = bt_fmemopen(metadata_buf, len_read, "rb"); | |
188 | if (!fp) { | |
087bc060 | 189 | BT_LOGE("Cannot memory-open metadata buffer: %s", |
7cdc2bab MD |
190 | strerror(errno)); |
191 | goto error; | |
192 | } | |
193 | ||
194 | decoder_status = ctf_metadata_decoder_decode(metadata->decoder, fp); | |
195 | switch (decoder_status) { | |
196 | case CTF_METADATA_DECODER_STATUS_OK: | |
197 | BT_PUT(trace->trace); | |
198 | trace->trace = ctf_metadata_decoder_get_trace(metadata->decoder); | |
199 | trace->new_metadata_needed = false; | |
200 | status = lttng_live_update_clock_map(trace); | |
201 | if (status != BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
202 | goto end; | |
203 | } | |
204 | break; | |
205 | case CTF_METADATA_DECODER_STATUS_INCOMPLETE: | |
206 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
207 | break; | |
208 | case CTF_METADATA_DECODER_STATUS_ERROR: | |
209 | case CTF_METADATA_DECODER_STATUS_INVAL_VERSION: | |
210 | case CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR: | |
211 | goto error; | |
212 | } | |
213 | ||
214 | goto end; | |
215 | error: | |
216 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
217 | end: | |
218 | if (fp) { | |
219 | int closeret; | |
220 | ||
221 | closeret = fclose(fp); | |
222 | if (closeret) { | |
087bc060 | 223 | BT_LOGE("Error on fclose"); |
7cdc2bab MD |
224 | } |
225 | } | |
226 | return status; | |
227 | } | |
228 | ||
229 | BT_HIDDEN | |
230 | int lttng_live_metadata_create_stream(struct lttng_live_session *session, | |
231 | uint64_t ctf_trace_id, | |
06994c71 MD |
232 | uint64_t stream_id, |
233 | const char *trace_name) | |
7cdc2bab MD |
234 | { |
235 | struct lttng_live_metadata *metadata = NULL; | |
236 | struct lttng_live_trace *trace; | |
06994c71 | 237 | const char *match; |
7cdc2bab MD |
238 | |
239 | metadata = g_new0(struct lttng_live_metadata, 1); | |
240 | if (!metadata) { | |
241 | return -1; | |
242 | } | |
243 | metadata->stream_id = stream_id; | |
244 | //TODO: add clock offset option | |
06994c71 MD |
245 | match = strstr(trace_name, session->session_name->str); |
246 | if (!match) { | |
247 | goto error; | |
248 | } | |
55314f2a | 249 | metadata->decoder = ctf_metadata_decoder_create(0, |
06994c71 | 250 | match); |
7cdc2bab MD |
251 | if (!metadata->decoder) { |
252 | goto error; | |
253 | } | |
254 | trace = lttng_live_ref_trace(session, ctf_trace_id); | |
255 | if (!trace) { | |
256 | goto error; | |
257 | } | |
258 | metadata->trace = trace; | |
259 | trace->metadata = metadata; | |
260 | return 0; | |
261 | ||
262 | error: | |
263 | ctf_metadata_decoder_destroy(metadata->decoder); | |
264 | g_free(metadata); | |
265 | return -1; | |
266 | } | |
267 | ||
268 | BT_HIDDEN | |
269 | void lttng_live_metadata_fini(struct lttng_live_trace *trace) | |
270 | { | |
271 | struct lttng_live_metadata *metadata = trace->metadata; | |
272 | ||
273 | if (!metadata) { | |
274 | return; | |
275 | } | |
276 | if (metadata->text) { | |
277 | free(metadata->text); | |
278 | } | |
279 | ctf_metadata_decoder_destroy(metadata->decoder); | |
280 | trace->metadata = NULL; | |
281 | lttng_live_unref_trace(trace); | |
282 | if (!metadata->closed) { | |
283 | lttng_live_unref_trace(metadata->trace); | |
284 | } | |
285 | g_free(metadata); | |
286 | } |