lttng-live: correctly handle ctrl-c and fix leaks
[babeltrace.git] / plugins / ctf / lttng-live / metadata.c
CommitLineData
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
43struct 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
56static
57enum 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;
89error:
90 status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR;
91end:
92 return status;
93}
94
95BT_HIDDEN
96enum 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
6f79a7cf
MD
120 if (!metadata->trace) {
121 trace->new_metadata_needed = false;
122 }
123
7cdc2bab
MD
124 if (!trace->new_metadata_needed) {
125 goto end;
126 }
127
128 /* Open for writing */
129 fp = bt_open_memstream(&metadata_buf, &size);
130 if (!fp) {
087bc060 131 BT_LOGE("Metadata open_memstream: %s", strerror(errno));
7cdc2bab
MD
132 goto error;
133 }
134
135 /* Grab all available metadata. */
136 do {
137 /*
138 * get_one_metadata_packet returns the number of bytes
139 * received, 0 when we have received everything, a
140 * negative value on error.
141 */
142 ret = lttng_live_get_one_metadata_packet(trace, fp);
143 if (ret > 0) {
144 len_read += ret;
145 }
146 } while (ret > 0);
147
148 /*
149 * Consider metadata closed as soon as we get an error reading
150 * it (e.g. cannot be found).
151 */
152 if (ret < 0) {
153 if (!metadata->closed) {
154 metadata->closed = true;
155 /*
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.
161 */
162 lttng_live_unref_trace(metadata->trace);
6f79a7cf 163 metadata->trace = NULL;
7cdc2bab 164 }
4c66436f 165 if (errno == EINTR) {
6f79a7cf 166 if (lttng_live_is_canceled(session->lttng_live)) {
4c66436f
MD
167 status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
168 goto end;
169 }
170 }
7cdc2bab
MD
171 }
172
173 if (bt_close_memstream(&metadata_buf, &size, fp)) {
174 BT_LOGE("bt_close_memstream: %s", strerror(errno));
175 }
176 ret = 0;
177 fp = NULL;
178
179 if (len_read == 0) {
180 if (!trace->trace) {
181 status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
182 goto end;
183 }
184 trace->new_metadata_needed = false;
185 goto end;
186 }
187
188 if (babeltrace_debug) {
189 // yydebug = 1;
190 }
191
192 fp = bt_fmemopen(metadata_buf, len_read, "rb");
193 if (!fp) {
087bc060 194 BT_LOGE("Cannot memory-open metadata buffer: %s",
7cdc2bab
MD
195 strerror(errno));
196 goto error;
197 }
198
199 decoder_status = ctf_metadata_decoder_decode(metadata->decoder, fp);
200 switch (decoder_status) {
201 case CTF_METADATA_DECODER_STATUS_OK:
202 BT_PUT(trace->trace);
203 trace->trace = ctf_metadata_decoder_get_trace(metadata->decoder);
204 trace->new_metadata_needed = false;
205 status = lttng_live_update_clock_map(trace);
206 if (status != BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK) {
207 goto end;
208 }
209 break;
210 case CTF_METADATA_DECODER_STATUS_INCOMPLETE:
211 status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
212 break;
213 case CTF_METADATA_DECODER_STATUS_ERROR:
214 case CTF_METADATA_DECODER_STATUS_INVAL_VERSION:
215 case CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR:
216 goto error;
217 }
218
219 goto end;
220error:
221 status = BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR;
222end:
223 if (fp) {
224 int closeret;
225
226 closeret = fclose(fp);
227 if (closeret) {
087bc060 228 BT_LOGE("Error on fclose");
7cdc2bab
MD
229 }
230 }
6f79a7cf 231 free(metadata_buf);
7cdc2bab
MD
232 return status;
233}
234
235BT_HIDDEN
236int lttng_live_metadata_create_stream(struct lttng_live_session *session,
237 uint64_t ctf_trace_id,
06994c71
MD
238 uint64_t stream_id,
239 const char *trace_name)
7cdc2bab
MD
240{
241 struct lttng_live_metadata *metadata = NULL;
242 struct lttng_live_trace *trace;
06994c71 243 const char *match;
7cdc2bab
MD
244
245 metadata = g_new0(struct lttng_live_metadata, 1);
246 if (!metadata) {
247 return -1;
248 }
249 metadata->stream_id = stream_id;
250 //TODO: add clock offset option
06994c71
MD
251 match = strstr(trace_name, session->session_name->str);
252 if (!match) {
253 goto error;
254 }
55314f2a 255 metadata->decoder = ctf_metadata_decoder_create(0,
06994c71 256 match);
7cdc2bab
MD
257 if (!metadata->decoder) {
258 goto error;
259 }
260 trace = lttng_live_ref_trace(session, ctf_trace_id);
261 if (!trace) {
262 goto error;
263 }
264 metadata->trace = trace;
265 trace->metadata = metadata;
266 return 0;
267
268error:
269 ctf_metadata_decoder_destroy(metadata->decoder);
270 g_free(metadata);
271 return -1;
272}
273
274BT_HIDDEN
275void lttng_live_metadata_fini(struct lttng_live_trace *trace)
276{
277 struct lttng_live_metadata *metadata = trace->metadata;
278
279 if (!metadata) {
280 return;
281 }
282 if (metadata->text) {
283 free(metadata->text);
284 }
285 ctf_metadata_decoder_destroy(metadata->decoder);
286 trace->metadata = NULL;
7cdc2bab
MD
287 if (!metadata->closed) {
288 lttng_live_unref_trace(metadata->trace);
289 }
290 g_free(metadata);
291}
This page took 0.035903 seconds and 4 git commands to generate.