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 | ||
26 | #include <stdio.h> | |
27 | #include <stdint.h> | |
28 | #include <stdlib.h> | |
29 | #include <stdbool.h> | |
30 | #include <glib.h> | |
31 | #include <babeltrace/compat/uuid-internal.h> | |
32 | #include <babeltrace/compat/memstream-internal.h> | |
4c66436f | 33 | #include <babeltrace/graph/graph.h> |
7cdc2bab | 34 | |
087bc060 | 35 | #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-LIVE-METADATA" |
7cdc2bab MD |
36 | |
37 | #include "metadata.h" | |
38 | #include "../common/metadata/decoder.h" | |
39 | ||
40 | #define TSDL_MAGIC 0x75d11d57 | |
41 | ||
42 | struct packet_header { | |
43 | uint32_t magic; | |
44 | uint8_t uuid[16]; | |
45 | uint32_t checksum; | |
46 | uint32_t content_size; | |
47 | uint32_t packet_size; | |
48 | uint8_t compression_scheme; | |
49 | uint8_t encryption_scheme; | |
50 | uint8_t checksum_scheme; | |
51 | uint8_t major; | |
52 | uint8_t minor; | |
53 | } __attribute__((__packed__)); | |
54 | ||
55 | static | |
56 | enum bt_ctf_lttng_live_iterator_status lttng_live_update_clock_map( | |
57 | struct lttng_live_trace *trace) | |
58 | { | |
59 | enum bt_ctf_lttng_live_iterator_status status = | |
60 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK; | |
61 | size_t i; | |
62 | int count, ret; | |
63 | ||
64 | BT_PUT(trace->cc_prio_map); | |
65 | trace->cc_prio_map = bt_clock_class_priority_map_create(); | |
66 | if (!trace->cc_prio_map) { | |
67 | goto error; | |
68 | } | |
69 | ||
70 | count = bt_ctf_trace_get_clock_class_count(trace->trace); | |
71 | assert(count >= 0); | |
72 | ||
73 | for (i = 0; i < count; i++) { | |
74 | struct bt_ctf_clock_class *clock_class = | |
75 | bt_ctf_trace_get_clock_class_by_index(trace->trace, i); | |
76 | ||
77 | assert(clock_class); | |
78 | ret = bt_clock_class_priority_map_add_clock_class( | |
79 | trace->cc_prio_map, clock_class, 0); | |
80 | BT_PUT(clock_class); | |
81 | ||
82 | if (ret) { | |
83 | goto error; | |
84 | } | |
85 | } | |
86 | ||
87 | goto end; | |
88 | error: | |
89 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
90 | end: | |
91 | return status; | |
92 | } | |
93 | ||
94 | BT_HIDDEN | |
95 | enum bt_ctf_lttng_live_iterator_status lttng_live_metadata_update( | |
96 | struct lttng_live_trace *trace) | |
97 | { | |
98 | struct lttng_live_session *session = trace->session; | |
7cdc2bab MD |
99 | struct lttng_live_metadata *metadata = trace->metadata; |
100 | ssize_t ret = 0; | |
101 | size_t size, len_read = 0; | |
102 | char *metadata_buf = NULL; | |
103 | FILE *fp = NULL; | |
104 | enum ctf_metadata_decoder_status decoder_status; | |
105 | enum bt_ctf_lttng_live_iterator_status status = | |
106 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK; | |
107 | ||
108 | /* No metadata stream yet. */ | |
109 | if (!metadata) { | |
110 | if (session->new_streams_needed) { | |
111 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
112 | } else { | |
113 | session->new_streams_needed = true; | |
114 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
115 | } | |
116 | goto end; | |
117 | } | |
118 | ||
119 | if (!trace->new_metadata_needed) { | |
120 | goto end; | |
121 | } | |
122 | ||
123 | /* Open for writing */ | |
124 | fp = bt_open_memstream(&metadata_buf, &size); | |
125 | if (!fp) { | |
087bc060 | 126 | BT_LOGE("Metadata open_memstream: %s", strerror(errno)); |
7cdc2bab MD |
127 | goto error; |
128 | } | |
129 | ||
130 | /* Grab all available metadata. */ | |
131 | do { | |
132 | /* | |
133 | * get_one_metadata_packet returns the number of bytes | |
134 | * received, 0 when we have received everything, a | |
135 | * negative value on error. | |
136 | */ | |
137 | ret = lttng_live_get_one_metadata_packet(trace, fp); | |
138 | if (ret > 0) { | |
139 | len_read += ret; | |
140 | } | |
141 | } while (ret > 0); | |
142 | ||
143 | /* | |
144 | * Consider metadata closed as soon as we get an error reading | |
145 | * it (e.g. cannot be found). | |
146 | */ | |
147 | if (ret < 0) { | |
148 | if (!metadata->closed) { | |
149 | metadata->closed = true; | |
150 | /* | |
151 | * Release our reference on the trace as soon as | |
152 | * we know the metadata stream is not available | |
153 | * anymore. This won't necessarily teardown the | |
154 | * metadata objects immediately, but only when | |
155 | * the data streams are done. | |
156 | */ | |
157 | lttng_live_unref_trace(metadata->trace); | |
158 | } | |
4c66436f MD |
159 | if (errno == EINTR) { |
160 | if (bt_graph_is_canceled(session->lttng_live->graph)) { | |
161 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
162 | goto end; | |
163 | } | |
164 | } | |
7cdc2bab MD |
165 | } |
166 | ||
167 | if (bt_close_memstream(&metadata_buf, &size, fp)) { | |
168 | BT_LOGE("bt_close_memstream: %s", strerror(errno)); | |
169 | } | |
170 | ret = 0; | |
171 | fp = NULL; | |
172 | ||
173 | if (len_read == 0) { | |
174 | if (!trace->trace) { | |
175 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
176 | goto end; | |
177 | } | |
178 | trace->new_metadata_needed = false; | |
179 | goto end; | |
180 | } | |
181 | ||
182 | if (babeltrace_debug) { | |
183 | // yydebug = 1; | |
184 | } | |
185 | ||
186 | fp = bt_fmemopen(metadata_buf, len_read, "rb"); | |
187 | if (!fp) { | |
087bc060 | 188 | BT_LOGE("Cannot memory-open metadata buffer: %s", |
7cdc2bab MD |
189 | strerror(errno)); |
190 | goto error; | |
191 | } | |
192 | ||
193 | decoder_status = ctf_metadata_decoder_decode(metadata->decoder, fp); | |
194 | switch (decoder_status) { | |
195 | case CTF_METADATA_DECODER_STATUS_OK: | |
196 | BT_PUT(trace->trace); | |
197 | trace->trace = ctf_metadata_decoder_get_trace(metadata->decoder); | |
198 | trace->new_metadata_needed = false; | |
199 | status = lttng_live_update_clock_map(trace); | |
200 | if (status != BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
201 | goto end; | |
202 | } | |
203 | break; | |
204 | case CTF_METADATA_DECODER_STATUS_INCOMPLETE: | |
205 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
206 | break; | |
207 | case CTF_METADATA_DECODER_STATUS_ERROR: | |
208 | case CTF_METADATA_DECODER_STATUS_INVAL_VERSION: | |
209 | case CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR: | |
210 | goto error; | |
211 | } | |
212 | ||
213 | goto end; | |
214 | error: | |
215 | status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
216 | end: | |
217 | if (fp) { | |
218 | int closeret; | |
219 | ||
220 | closeret = fclose(fp); | |
221 | if (closeret) { | |
087bc060 | 222 | BT_LOGE("Error on fclose"); |
7cdc2bab MD |
223 | } |
224 | } | |
225 | return status; | |
226 | } | |
227 | ||
228 | BT_HIDDEN | |
229 | int lttng_live_metadata_create_stream(struct lttng_live_session *session, | |
230 | uint64_t ctf_trace_id, | |
231 | uint64_t stream_id) | |
232 | { | |
233 | struct lttng_live_metadata *metadata = NULL; | |
234 | struct lttng_live_trace *trace; | |
235 | ||
236 | metadata = g_new0(struct lttng_live_metadata, 1); | |
237 | if (!metadata) { | |
238 | return -1; | |
239 | } | |
240 | metadata->stream_id = stream_id; | |
241 | //TODO: add clock offset option | |
1a9f7075 PP |
242 | //TODO: add (preferably unique) trace's name |
243 | metadata->decoder = ctf_metadata_decoder_create(stderr, 0, | |
244 | "lttng-live"); | |
7cdc2bab MD |
245 | if (!metadata->decoder) { |
246 | goto error; | |
247 | } | |
248 | trace = lttng_live_ref_trace(session, ctf_trace_id); | |
249 | if (!trace) { | |
250 | goto error; | |
251 | } | |
252 | metadata->trace = trace; | |
253 | trace->metadata = metadata; | |
254 | return 0; | |
255 | ||
256 | error: | |
257 | ctf_metadata_decoder_destroy(metadata->decoder); | |
258 | g_free(metadata); | |
259 | return -1; | |
260 | } | |
261 | ||
262 | BT_HIDDEN | |
263 | void lttng_live_metadata_fini(struct lttng_live_trace *trace) | |
264 | { | |
265 | struct lttng_live_metadata *metadata = trace->metadata; | |
266 | ||
267 | if (!metadata) { | |
268 | return; | |
269 | } | |
270 | if (metadata->text) { | |
271 | free(metadata->text); | |
272 | } | |
273 | ctf_metadata_decoder_destroy(metadata->decoder); | |
274 | trace->metadata = NULL; | |
275 | lttng_live_unref_trace(trace); | |
276 | if (!metadata->closed) { | |
277 | lttng_live_unref_trace(metadata->trace); | |
278 | } | |
279 | g_free(metadata); | |
280 | } |