| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 5 | * Copyright 2016 Philippe Proulx <pproulx@efficios.com> |
| 6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 | */ |
| 8 | |
| 9 | #define BT_COMP_LOG_SELF_COMP self_comp |
| 10 | #define BT_LOG_OUTPUT_LEVEL log_level |
| 11 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/META" |
| 12 | #include "logging/comp-logging.h" |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <stdint.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <stdbool.h> |
| 18 | #include <glib.h> |
| 19 | #include "compat/memstream.h" |
| 20 | #include <babeltrace2/babeltrace.h> |
| 21 | |
| 22 | #include "metadata.h" |
| 23 | #include "../common/metadata/decoder.h" |
| 24 | #include "../common/metadata/ctf-meta-configure-ir-trace.h" |
| 25 | |
| 26 | #define TSDL_MAGIC 0x75d11d57 |
| 27 | |
| 28 | struct packet_header { |
| 29 | uint32_t magic; |
| 30 | uint8_t uuid[16]; |
| 31 | uint32_t checksum; |
| 32 | uint32_t content_size; |
| 33 | uint32_t packet_size; |
| 34 | uint8_t compression_scheme; |
| 35 | uint8_t encryption_scheme; |
| 36 | uint8_t checksum_scheme; |
| 37 | uint8_t major; |
| 38 | uint8_t minor; |
| 39 | } __attribute__((__packed__)); |
| 40 | |
| 41 | |
| 42 | static |
| 43 | bool stream_classes_all_have_default_clock_class(bt_trace_class *tc, |
| 44 | bt_logging_level log_level, |
| 45 | bt_self_component *self_comp) |
| 46 | { |
| 47 | uint64_t i, sc_count; |
| 48 | const bt_clock_class *cc = NULL; |
| 49 | const bt_stream_class *sc; |
| 50 | bool ret = true; |
| 51 | |
| 52 | sc_count = bt_trace_class_get_stream_class_count(tc); |
| 53 | for (i = 0; i < sc_count; i++) { |
| 54 | sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i); |
| 55 | |
| 56 | BT_ASSERT(sc); |
| 57 | |
| 58 | cc = bt_stream_class_borrow_default_clock_class_const(sc); |
| 59 | if (!cc) { |
| 60 | ret = false; |
| 61 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 62 | "Stream class doesn't have a default clock class: " |
| 63 | "sc-id=%" PRIu64 ", sc-name=\"%s\"", |
| 64 | bt_stream_class_get_id(sc), |
| 65 | bt_stream_class_get_name(sc)); |
| 66 | goto end; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | end: |
| 71 | return ret; |
| 72 | } |
| 73 | /* |
| 74 | * Iterate over the stream classes and returns the first clock class |
| 75 | * encountered. This is useful to create message iterator inactivity message as |
| 76 | * we don't need a particular clock class. |
| 77 | */ |
| 78 | static |
| 79 | const bt_clock_class *borrow_any_clock_class(bt_trace_class *tc) |
| 80 | { |
| 81 | uint64_t i, sc_count; |
| 82 | const bt_clock_class *cc = NULL; |
| 83 | const bt_stream_class *sc; |
| 84 | |
| 85 | sc_count = bt_trace_class_get_stream_class_count(tc); |
| 86 | for (i = 0; i < sc_count; i++) { |
| 87 | sc = bt_trace_class_borrow_stream_class_by_index_const(tc, i); |
| 88 | BT_ASSERT_DBG(sc); |
| 89 | |
| 90 | cc = bt_stream_class_borrow_default_clock_class_const(sc); |
| 91 | if (cc) { |
| 92 | goto end; |
| 93 | } |
| 94 | } |
| 95 | end: |
| 96 | BT_ASSERT_DBG(cc); |
| 97 | return cc; |
| 98 | } |
| 99 | |
| 100 | BT_HIDDEN |
| 101 | enum lttng_live_iterator_status lttng_live_metadata_update( |
| 102 | struct lttng_live_trace *trace) |
| 103 | { |
| 104 | struct lttng_live_session *session = trace->session; |
| 105 | struct lttng_live_metadata *metadata = trace->metadata; |
| 106 | size_t size, len_read = 0; |
| 107 | char *metadata_buf = NULL; |
| 108 | bool keep_receiving; |
| 109 | FILE *fp = NULL; |
| 110 | enum ctf_metadata_decoder_status decoder_status; |
| 111 | enum lttng_live_iterator_status status = |
| 112 | LTTNG_LIVE_ITERATOR_STATUS_OK; |
| 113 | bt_logging_level log_level = trace->log_level; |
| 114 | bt_self_component *self_comp = trace->self_comp; |
| 115 | enum lttng_live_get_one_metadata_status metadata_status; |
| 116 | |
| 117 | BT_COMP_LOGD("Updating metadata for trace: trace-id=%"PRIu64, trace->id); |
| 118 | |
| 119 | /* No metadata stream yet. */ |
| 120 | if (!metadata) { |
| 121 | if (session->new_streams_needed) { |
| 122 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
| 123 | } else { |
| 124 | session->new_streams_needed = true; |
| 125 | status = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
| 126 | } |
| 127 | goto end; |
| 128 | } |
| 129 | |
| 130 | if (trace->metadata_stream_state != LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) { |
| 131 | goto end; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Open a new write only file handle to populate the `metadata_buf` |
| 136 | * memory buffer so we can write in loop in it easily. |
| 137 | */ |
| 138 | fp = bt_open_memstream(&metadata_buf, &size); |
| 139 | if (!fp) { |
| 140 | if (errno == EINTR && |
| 141 | lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) { |
| 142 | session->lttng_live_msg_iter->was_interrupted = true; |
| 143 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
| 144 | } else { |
| 145 | BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp, |
| 146 | "Metadata open_memstream", "."); |
| 147 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
| 148 | } |
| 149 | goto end; |
| 150 | } |
| 151 | |
| 152 | keep_receiving = true; |
| 153 | /* Grab all available metadata. */ |
| 154 | while (keep_receiving) { |
| 155 | size_t reply_len = 0; |
| 156 | /* |
| 157 | * lttng_live_get_one_metadata_packet() asks the Relay Daemon |
| 158 | * for new metadata. If new metadata is received, the function |
| 159 | * writes it to the provided file handle and updates the |
| 160 | * reply_len output parameter. We call this function in loop |
| 161 | * until it returns _END meaning that no new metadata is |
| 162 | * available. |
| 163 | * We may receive a _CLOSED status if the metadata stream we |
| 164 | * are requesting is no longer available on the relay. |
| 165 | * If we receive an _ERROR status, it means there was a |
| 166 | * networking, allocating, or some other unrecoverable error. |
| 167 | */ |
| 168 | metadata_status = lttng_live_get_one_metadata_packet(trace, fp, |
| 169 | &reply_len); |
| 170 | |
| 171 | switch (metadata_status) { |
| 172 | case LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK: |
| 173 | len_read += reply_len; |
| 174 | break; |
| 175 | case LTTNG_LIVE_GET_ONE_METADATA_STATUS_END: |
| 176 | keep_receiving = false; |
| 177 | break; |
| 178 | case LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED: |
| 179 | BT_COMP_LOGD("Metadata stream was closed by the Relay, the trace is no longer active: " |
| 180 | "trace-id=%"PRIu64", metadata-stream-id=%"PRIu64, |
| 181 | trace->id, metadata->stream_id); |
| 182 | /* |
| 183 | * The stream was closed and we received everything |
| 184 | * there was to receive for this metadata stream. |
| 185 | * We go on with the decoding of what we received. So |
| 186 | * that data stream can be decoded. |
| 187 | */ |
| 188 | keep_receiving = false; |
| 189 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED; |
| 190 | break; |
| 191 | case LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR: |
| 192 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 193 | "Error getting one trace metadata packet: " |
| 194 | "trace-id=%"PRIu64, trace->id); |
| 195 | goto error; |
| 196 | default: |
| 197 | bt_common_abort(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* The memory buffer `metadata_buf` contains all the metadata. */ |
| 202 | if (bt_close_memstream(&metadata_buf, &size, fp)) { |
| 203 | BT_COMP_LOGW_ERRNO("Metadata bt_close_memstream", "."); |
| 204 | } |
| 205 | |
| 206 | fp = NULL; |
| 207 | |
| 208 | if (len_read == 0) { |
| 209 | if (!trace->trace) { |
| 210 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
| 211 | goto end; |
| 212 | } |
| 213 | |
| 214 | /* The relay sent zero bytes of metdata. */ |
| 215 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED; |
| 216 | goto end; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Open a new reading file handle on the `metadata_buf` and pass it to |
| 221 | * the metadata decoder. |
| 222 | */ |
| 223 | fp = bt_fmemopen(metadata_buf, len_read, "rb"); |
| 224 | if (!fp) { |
| 225 | if (errno == EINTR && |
| 226 | lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) { |
| 227 | session->lttng_live_msg_iter->was_interrupted = true; |
| 228 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
| 229 | } else { |
| 230 | BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp, |
| 231 | "Cannot memory-open metadata buffer", "."); |
| 232 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
| 233 | } |
| 234 | goto end; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * The call to ctf_metadata_decoder_append_content() will append |
| 239 | * new metadata to our current trace class. |
| 240 | */ |
| 241 | BT_COMP_LOGD("Appending new metadata to the ctf_trace class"); |
| 242 | decoder_status = ctf_metadata_decoder_append_content( |
| 243 | metadata->decoder, fp); |
| 244 | switch (decoder_status) { |
| 245 | case CTF_METADATA_DECODER_STATUS_OK: |
| 246 | if (!trace->trace_class) { |
| 247 | struct ctf_trace_class *tc = |
| 248 | ctf_metadata_decoder_borrow_ctf_trace_class( |
| 249 | metadata->decoder); |
| 250 | |
| 251 | trace->trace_class = |
| 252 | ctf_metadata_decoder_get_ir_trace_class( |
| 253 | metadata->decoder); |
| 254 | trace->trace = bt_trace_create(trace->trace_class); |
| 255 | if (!trace->trace) { |
| 256 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 257 | "Failed to create bt_trace"); |
| 258 | goto error; |
| 259 | } |
| 260 | if (ctf_trace_class_configure_ir_trace(tc, |
| 261 | trace->trace)) { |
| 262 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 263 | "Failed to configure ctf trace class"); |
| 264 | goto error; |
| 265 | } |
| 266 | if (!stream_classes_all_have_default_clock_class( |
| 267 | trace->trace_class, log_level, |
| 268 | self_comp)) { |
| 269 | /* Error logged in function. */ |
| 270 | goto error; |
| 271 | } |
| 272 | trace->clock_class = |
| 273 | borrow_any_clock_class(trace->trace_class); |
| 274 | } |
| 275 | |
| 276 | /* The metadata was updated succesfully. */ |
| 277 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED; |
| 278 | |
| 279 | break; |
| 280 | case CTF_METADATA_DECODER_STATUS_INCOMPLETE: |
| 281 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
| 282 | break; |
| 283 | default: |
| 284 | goto error; |
| 285 | } |
| 286 | |
| 287 | goto end; |
| 288 | |
| 289 | error: |
| 290 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
| 291 | end: |
| 292 | if (fp) { |
| 293 | int closeret; |
| 294 | |
| 295 | closeret = fclose(fp); |
| 296 | if (closeret) { |
| 297 | BT_COMP_LOGW_ERRNO("Error on fclose", "."); |
| 298 | } |
| 299 | } |
| 300 | free(metadata_buf); |
| 301 | return status; |
| 302 | } |
| 303 | |
| 304 | BT_HIDDEN |
| 305 | int lttng_live_metadata_create_stream(struct lttng_live_session *session, |
| 306 | uint64_t ctf_trace_id, uint64_t stream_id, |
| 307 | const char *trace_name) |
| 308 | { |
| 309 | bt_self_component *self_comp = session->self_comp; |
| 310 | bt_logging_level log_level = session->log_level; |
| 311 | struct lttng_live_metadata *metadata = NULL; |
| 312 | struct lttng_live_trace *trace; |
| 313 | struct ctf_metadata_decoder_config cfg = { |
| 314 | .log_level = session->log_level, |
| 315 | .self_comp = session->self_comp, |
| 316 | .clock_class_offset_s = 0, |
| 317 | .clock_class_offset_ns = 0, |
| 318 | .create_trace_class = true, |
| 319 | }; |
| 320 | |
| 321 | metadata = g_new0(struct lttng_live_metadata, 1); |
| 322 | if (!metadata) { |
| 323 | return -1; |
| 324 | } |
| 325 | metadata->log_level = session->log_level; |
| 326 | metadata->self_comp = session->self_comp; |
| 327 | metadata->stream_id = stream_id; |
| 328 | |
| 329 | metadata->decoder = ctf_metadata_decoder_create(&cfg); |
| 330 | if (!metadata->decoder) { |
| 331 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 332 | "Failed to create CTF metadata decoder"); |
| 333 | goto error; |
| 334 | } |
| 335 | trace = lttng_live_session_borrow_or_create_trace_by_id(session, |
| 336 | ctf_trace_id); |
| 337 | if (!trace) { |
| 338 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 339 | "Failed to borrow trace"); |
| 340 | goto error; |
| 341 | } |
| 342 | trace->metadata = metadata; |
| 343 | return 0; |
| 344 | |
| 345 | error: |
| 346 | ctf_metadata_decoder_destroy(metadata->decoder); |
| 347 | g_free(metadata); |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | BT_HIDDEN |
| 352 | void lttng_live_metadata_fini(struct lttng_live_trace *trace) |
| 353 | { |
| 354 | struct lttng_live_metadata *metadata = trace->metadata; |
| 355 | |
| 356 | if (!metadata) { |
| 357 | return; |
| 358 | } |
| 359 | ctf_metadata_decoder_destroy(metadata->decoder); |
| 360 | trace->metadata = NULL; |
| 361 | g_free(metadata); |
| 362 | } |