src.ctf.fs: make ctf_visitor_generate_ir::trace_class a bt2::TraceClass::Shared
[babeltrace.git] / src / plugins / ctf / common / src / metadata / tsdl / decoder.hpp
CommitLineData
1e649dff 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
1e649dff 3 *
0235b0db 4 * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com>
1e649dff
PP
5 */
6
0235b0db
MJ
7#ifndef _METADATA_DECODER_H
8#define _METADATA_DECODER_H
9
087cd0f5 10#include <stdio.h>
1e649dff 11
3fadfbc0 12#include <babeltrace2/babeltrace.h>
1e649dff 13
06be9946 14#include "common/uuid.h"
70923576 15#include "cpp-common/bt2/trace-ir.hpp"
0f5c5d5c
SM
16#include "cpp-common/bt2c/logging.hpp"
17#include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
06be9946 18
c942e7a2
SM
19#include "../../../src/clk-cls-cfg.hpp"
20
21/* A CTF metadata decoder object */
22struct ctf_metadata_decoder;
23
1e649dff 24/* CTF metadata decoder status */
4164020e
SM
25enum ctf_metadata_decoder_status
26{
27 CTF_METADATA_DECODER_STATUS_OK = 0,
28 CTF_METADATA_DECODER_STATUS_NONE = 1,
29 CTF_METADATA_DECODER_STATUS_ERROR = -1,
30 CTF_METADATA_DECODER_STATUS_INCOMPLETE = -2,
31 CTF_METADATA_DECODER_STATUS_INVAL_VERSION = -3,
32 CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR = -4,
1e649dff
PP
33};
34
0f5c5d5c
SM
35inline const char *format_as(ctf_metadata_decoder_status status) noexcept
36{
37 switch (status) {
38 case CTF_METADATA_DECODER_STATUS_OK:
39 return "CTF_METADATA_DECODER_STATUS_OK";
40
41 case CTF_METADATA_DECODER_STATUS_NONE:
42 return "CTF_METADATA_DECODER_STATUS_NONE";
43
44 case CTF_METADATA_DECODER_STATUS_ERROR:
45 return "CTF_METADATA_DECODER_STATUS_ERROR";
46
47 case CTF_METADATA_DECODER_STATUS_INCOMPLETE:
48 return "CTF_METADATA_DECODER_STATUS_INCOMPLETE";
49
50 case CTF_METADATA_DECODER_STATUS_INVAL_VERSION:
51 return "CTF_METADATA_DECODER_STATUS_INVAL_VERSION";
52
53 case CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR:
54 return "CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR";
55 }
56
57 bt_common_abort();
58}
59
a2a54545 60/* Decoding configuration */
4164020e
SM
61struct ctf_metadata_decoder_config
62{
0f5c5d5c
SM
63 explicit ctf_metadata_decoder_config(const bt2c::Logger& parentLogger) :
64 logger {parentLogger, "PLUGIN/CTF/META/DECODER-CONFIG"}
65 {
66 }
4164020e 67
0f5c5d5c
SM
68 bt2c::Logger logger;
69
70 /* Weak, used to create a bt_trace_class, if not nullptr. */
afb0f12b 71 bt_self_component *self_comp = nullptr;
4164020e 72
c942e7a2 73 ctf::src::ClkClsCfg clkClsCfg;
4164020e
SM
74
75 /* True to create trace class objects */
afb0f12b 76 bool create_trace_class = false;
4164020e
SM
77
78 /*
79 * True to keep the plain text when content is appended with
80 * ctf_metadata_decoder_append_content().
81 */
afb0f12b 82 bool keep_plain_text = false;
a2a54545
PP
83};
84
1fa280c9
SM
85struct ctf_metadata_decoder_deleter
86{
87 void operator()(struct ctf_metadata_decoder *decoder);
88};
89
90using ctf_metadata_decoder_up = std::unique_ptr<ctf_metadata_decoder, ctf_metadata_decoder_deleter>;
91
1e649dff 92/*
862ca4ed 93 * Creates a CTF metadata decoder.
1e649dff
PP
94 *
95 * Returns `NULL` on error.
96 */
1fa280c9 97ctf_metadata_decoder_up
4164020e 98ctf_metadata_decoder_create(const struct ctf_metadata_decoder_config *config);
1e649dff
PP
99
100/*
101 * Destroys a CTF metadata decoder that you created with
102 * ctf_metadata_decoder_create().
103 */
4164020e 104void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder *metadata_decoder);
1e649dff
PP
105
106/*
06be9946 107 * Appends content to the metadata decoder.
1e649dff
PP
108 *
109 * This function reads the metadata from the current position of `fp`
06be9946 110 * until the end of this file stream.
1e649dff
PP
111 *
112 * The metadata can be packetized or not.
113 *
06be9946
PP
114 * The metadata chunk needs to be complete and lexically scannable, that
115 * is, zero or more complete top-level blocks. If it's incomplete, this
1e649dff
PP
116 * function returns `CTF_METADATA_DECODER_STATUS_INCOMPLETE`. If this
117 * function returns `CTF_METADATA_DECODER_STATUS_INCOMPLETE`, then you
06be9946 118 * need to call it again with the _same_ metadata and more to make it
1e649dff
PP
119 * complete. For example:
120 *
121 * First call: event { name = hell
122 * Second call: event { name = hello_world; ... };
123 *
1e649dff
PP
124 * If everything goes as expected, this function returns
125 * `CTF_METADATA_DECODER_STATUS_OK`.
126 */
4164020e
SM
127enum ctf_metadata_decoder_status
128ctf_metadata_decoder_append_content(struct ctf_metadata_decoder *metadata_decoder, FILE *fp);
1e649dff 129
06be9946
PP
130/*
131 * Returns the trace IR trace class of this metadata decoder (new
132 * reference).
133 *
134 * Returns `NULL` if there's none yet or if the metadata decoder is not
135 * configured to create trace classes.
136 */
70923576 137bt2::TraceClass::Shared ctf_metadata_decoder_get_ir_trace_class(struct ctf_metadata_decoder *mdec);
44c440bc 138
06be9946
PP
139/*
140 * Returns the CTF IR trace class of this metadata decoder.
141 *
142 * Returns `NULL` if there's none yet or if the metadata decoder is not
143 * configured to create trace classes.
144 */
4164020e
SM
145struct ctf_trace_class *
146ctf_metadata_decoder_borrow_ctf_trace_class(struct ctf_metadata_decoder *mdec);
1e649dff
PP
147
148/*
06be9946
PP
149 * Checks whether or not a given metadata file stream `fp` is
150 * packetized, setting `is_packetized` accordingly on success. On
151 * success, also sets `*byte_order` to the byte order of the first
152 * packet.
1e649dff 153 */
4164020e 154int ctf_metadata_decoder_is_packetized(FILE *fp, bool *is_packetized, int *byte_order,
0f5c5d5c 155 const bt2c::Logger& logger);
1e649dff
PP
156
157/*
06be9946
PP
158 * Returns the byte order of the decoder's metadata stream as set by the
159 * last call to ctf_metadata_decoder_append_content().
160 *
161 * Returns -1 if unknown (plain text content).
1e649dff 162 */
06be9946
PP
163int ctf_metadata_decoder_get_byte_order(struct ctf_metadata_decoder *mdec);
164
165/*
166 * Returns the UUID of the decoder's metadata stream as set by the last
167 * call to ctf_metadata_decoder_append_content().
1a6da3f9 168 */
4164020e 169int ctf_metadata_decoder_get_uuid(struct ctf_metadata_decoder *mdec, bt_uuid_t uuid);
1a6da3f9
PP
170
171/*
172 * Returns the UUID of the decoder's trace class, if available.
06be9946 173 *
1a6da3f9
PP
174 * Returns:
175 *
176 * * `CTF_METADATA_DECODER_STATUS_OK`: success.
177 * * `CTF_METADATA_DECODER_STATUS_NONE`: no UUID.
178 * * `CTF_METADATA_DECODER_STATUS_INCOMPLETE`: missing metadata content.
06be9946 179 */
4164020e
SM
180enum ctf_metadata_decoder_status
181ctf_metadata_decoder_get_trace_class_uuid(struct ctf_metadata_decoder *mdec, bt_uuid_t uuid);
06be9946
PP
182
183/*
184 * Returns the metadata decoder's current metadata text.
185 */
06be9946 186const char *ctf_metadata_decoder_get_text(struct ctf_metadata_decoder *mdec);
1e649dff 187
4164020e
SM
188static inline bool ctf_metadata_decoder_is_packet_version_valid(unsigned int major,
189 unsigned int minor)
3c8252a5 190{
4164020e 191 return major == 1 && minor == 8;
3c8252a5
PP
192}
193
1e649dff 194#endif /* _METADATA_DECODER_H */
This page took 0.102058 seconds and 4 git commands to generate.