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