| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2014 EfficiOS Inc. |
| 5 | * |
| 6 | * The Common Trace Format (CTF) Specification is available at |
| 7 | * http://www.efficios.com/ctf |
| 8 | */ |
| 9 | |
| 10 | #ifndef BABELTRACE_CTF_WRITER_TRACE_H |
| 11 | #define BABELTRACE_CTF_WRITER_TRACE_H |
| 12 | |
| 13 | #include "common/macros.h" |
| 14 | #include "common/uuid.h" |
| 15 | #include <babeltrace2-ctf-writer/field-types.h> |
| 16 | #include <babeltrace2-ctf-writer/fields.h> |
| 17 | #include <babeltrace2-ctf-writer/trace.h> |
| 18 | #include <glib.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include "assert-pre.h" |
| 22 | #include "attributes.h" |
| 23 | #include "clock-class.h" |
| 24 | #include "object.h" |
| 25 | #include "stream-class.h" |
| 26 | #include "validation.h" |
| 27 | #include "values.h" |
| 28 | |
| 29 | struct bt_ctf_trace_common { |
| 30 | struct bt_ctf_object base; |
| 31 | GString *name; |
| 32 | int frozen; |
| 33 | bt_uuid_t uuid; |
| 34 | bt_ctf_bool uuid_set; |
| 35 | enum bt_ctf_byte_order native_byte_order; |
| 36 | struct bt_ctf_private_value *environment; |
| 37 | GPtrArray *clock_classes; /* Array of pointers to bt_ctf_clock_class */ |
| 38 | GPtrArray *stream_classes; /* Array of ptrs to bt_ctf_stream_class_common */ |
| 39 | GPtrArray *streams; /* Array of ptrs to bt_ctf_stream_common */ |
| 40 | struct bt_ctf_field_type_common *packet_header_field_type; |
| 41 | int64_t next_stream_id; |
| 42 | |
| 43 | /* |
| 44 | * This flag indicates if the trace is valid. A valid |
| 45 | * trace is _always_ frozen. |
| 46 | */ |
| 47 | int valid; |
| 48 | }; |
| 49 | |
| 50 | bt_ctf_bool bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common *trace, |
| 51 | struct bt_ctf_clock_class *clock_class); |
| 52 | |
| 53 | int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common *trace, |
| 54 | bt_ctf_object_release_func release_func); |
| 55 | |
| 56 | void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common *trace); |
| 57 | |
| 58 | static inline |
| 59 | const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common *trace) |
| 60 | { |
| 61 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 62 | return trace->name ? trace->name->str : NULL; |
| 63 | } |
| 64 | |
| 65 | int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common *trace, const char *name); |
| 66 | |
| 67 | static inline |
| 68 | const uint8_t *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common *trace) |
| 69 | { |
| 70 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 71 | return trace->uuid_set ? trace->uuid : NULL; |
| 72 | } |
| 73 | |
| 74 | int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common *trace, const uint8_t *uuid); |
| 75 | |
| 76 | int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common *trace, |
| 77 | const char *name, struct bt_ctf_private_value *value); |
| 78 | |
| 79 | int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common *trace, |
| 80 | const char *name, const char *value); |
| 81 | |
| 82 | int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common *trace, |
| 83 | const char *name, int64_t value); |
| 84 | |
| 85 | static inline |
| 86 | int64_t bt_ctf_trace_common_get_environment_field_count( |
| 87 | struct bt_ctf_trace_common *trace) |
| 88 | { |
| 89 | int64_t ret; |
| 90 | |
| 91 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 92 | ret = bt_ctf_attributes_get_count(trace->environment); |
| 93 | BT_ASSERT_DBG(ret >= 0); |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | static inline |
| 98 | const char * |
| 99 | bt_ctf_trace_common_get_environment_field_name_by_index( |
| 100 | struct bt_ctf_trace_common *trace, uint64_t index) |
| 101 | { |
| 102 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 103 | return bt_ctf_attributes_get_field_name(trace->environment, index); |
| 104 | } |
| 105 | |
| 106 | static inline |
| 107 | struct bt_ctf_private_value * |
| 108 | bt_ctf_trace_common_borrow_environment_field_value_by_index( |
| 109 | struct bt_ctf_trace_common *trace, uint64_t index) |
| 110 | { |
| 111 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 112 | return bt_ctf_attributes_borrow_field_value(trace->environment, index); |
| 113 | } |
| 114 | |
| 115 | static inline |
| 116 | struct bt_ctf_private_value * |
| 117 | bt_ctf_trace_common_borrow_environment_field_value_by_name( |
| 118 | struct bt_ctf_trace_common *trace, const char *name) |
| 119 | { |
| 120 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 121 | BT_CTF_ASSERT_PRE_NON_NULL(name, "Name"); |
| 122 | return bt_ctf_attributes_borrow_field_value_by_name(trace->environment, |
| 123 | name); |
| 124 | } |
| 125 | |
| 126 | int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common *trace, |
| 127 | struct bt_ctf_clock_class *clock_class); |
| 128 | |
| 129 | static inline |
| 130 | int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common *trace) |
| 131 | { |
| 132 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 133 | return trace->clock_classes->len; |
| 134 | } |
| 135 | |
| 136 | static inline |
| 137 | struct bt_ctf_clock_class *bt_ctf_trace_common_borrow_clock_class_by_index( |
| 138 | struct bt_ctf_trace_common *trace, uint64_t index) |
| 139 | { |
| 140 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 141 | BT_CTF_ASSERT_PRE(index < trace->clock_classes->len, |
| 142 | "Index is out of bounds: index=%" PRIu64 ", " |
| 143 | "count=%u", |
| 144 | index, trace->clock_classes->len); |
| 145 | return g_ptr_array_index(trace->clock_classes, index); |
| 146 | } |
| 147 | |
| 148 | static inline |
| 149 | int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common *trace) |
| 150 | { |
| 151 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 152 | return (int64_t) trace->streams->len; |
| 153 | } |
| 154 | |
| 155 | static inline |
| 156 | struct bt_ctf_stream_common *bt_ctf_trace_common_borrow_stream_by_index( |
| 157 | struct bt_ctf_trace_common *trace, |
| 158 | uint64_t index) |
| 159 | { |
| 160 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 161 | BT_CTF_ASSERT_PRE(index < trace->streams->len, |
| 162 | "Index is out of bounds: index=%" PRIu64 ", " |
| 163 | "count=%u", |
| 164 | index, trace->streams->len); |
| 165 | return g_ptr_array_index(trace->streams, index); |
| 166 | } |
| 167 | |
| 168 | static inline |
| 169 | int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common *trace) |
| 170 | { |
| 171 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 172 | return (int64_t) trace->stream_classes->len; |
| 173 | } |
| 174 | |
| 175 | static inline |
| 176 | struct bt_ctf_stream_class_common *bt_ctf_trace_common_borrow_stream_class_by_index( |
| 177 | struct bt_ctf_trace_common *trace, uint64_t index) |
| 178 | { |
| 179 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 180 | BT_CTF_ASSERT_PRE(index < trace->stream_classes->len, |
| 181 | "Index is out of bounds: index=%" PRIu64 ", " |
| 182 | "count=%u", |
| 183 | index, trace->stream_classes->len); |
| 184 | return g_ptr_array_index(trace->stream_classes, index); |
| 185 | } |
| 186 | |
| 187 | static inline |
| 188 | struct bt_ctf_stream_class_common *bt_ctf_trace_common_borrow_stream_class_by_id( |
| 189 | struct bt_ctf_trace_common *trace, uint64_t id_param) |
| 190 | { |
| 191 | int i; |
| 192 | struct bt_ctf_stream_class_common *stream_class = NULL; |
| 193 | int64_t id = (int64_t) id_param; |
| 194 | |
| 195 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 196 | BT_CTF_ASSERT_PRE(id >= 0, |
| 197 | "Invalid stream class ID: %" PRIu64, id_param); |
| 198 | |
| 199 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 200 | struct bt_ctf_stream_class_common *stream_class_candidate; |
| 201 | |
| 202 | stream_class_candidate = |
| 203 | g_ptr_array_index(trace->stream_classes, i); |
| 204 | |
| 205 | if (bt_ctf_stream_class_common_get_id(stream_class_candidate) == |
| 206 | (int64_t) id) { |
| 207 | stream_class = stream_class_candidate; |
| 208 | goto end; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | end: |
| 213 | return stream_class; |
| 214 | } |
| 215 | |
| 216 | static inline |
| 217 | struct bt_ctf_clock_class *bt_ctf_trace_common_borrow_clock_class_by_name( |
| 218 | struct bt_ctf_trace_common *trace, const char *name) |
| 219 | { |
| 220 | size_t i; |
| 221 | struct bt_ctf_clock_class *clock_class = NULL; |
| 222 | |
| 223 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 224 | BT_CTF_ASSERT_PRE_NON_NULL(name, "Name"); |
| 225 | |
| 226 | for (i = 0; i < trace->clock_classes->len; i++) { |
| 227 | struct bt_ctf_clock_class *cur_clk = |
| 228 | g_ptr_array_index(trace->clock_classes, i); |
| 229 | const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk); |
| 230 | |
| 231 | if (!cur_clk_name) { |
| 232 | goto end; |
| 233 | } |
| 234 | |
| 235 | if (!strcmp(cur_clk_name, name)) { |
| 236 | clock_class = cur_clk; |
| 237 | goto end; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | end: |
| 242 | return clock_class; |
| 243 | } |
| 244 | |
| 245 | static inline |
| 246 | enum bt_ctf_byte_order bt_ctf_trace_common_get_native_byte_order( |
| 247 | struct bt_ctf_trace_common *trace) |
| 248 | { |
| 249 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 250 | return trace->native_byte_order; |
| 251 | } |
| 252 | |
| 253 | int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common *trace, |
| 254 | enum bt_ctf_byte_order byte_order, bool allow_unspecified); |
| 255 | |
| 256 | static inline |
| 257 | struct bt_ctf_field_type_common *bt_ctf_trace_common_borrow_packet_header_field_type( |
| 258 | struct bt_ctf_trace_common *trace) |
| 259 | { |
| 260 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 261 | return trace->packet_header_field_type; |
| 262 | } |
| 263 | |
| 264 | int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common *trace, |
| 265 | struct bt_ctf_field_type_common *packet_header_field_type); |
| 266 | |
| 267 | static inline |
| 268 | void bt_ctf_trace_common_freeze(struct bt_ctf_trace_common *trace) |
| 269 | { |
| 270 | int i; |
| 271 | |
| 272 | if (trace->frozen) { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | BT_LOGD("Freezing trace: addr=%p, name=\"%s\"", |
| 277 | trace, bt_ctf_trace_common_get_name(trace)); |
| 278 | BT_LOGD_STR("Freezing packet header field type."); |
| 279 | bt_ctf_field_type_common_freeze(trace->packet_header_field_type); |
| 280 | BT_LOGD_STR("Freezing environment attributes."); |
| 281 | bt_ctf_attributes_freeze(trace->environment); |
| 282 | |
| 283 | if (trace->clock_classes->len > 0) { |
| 284 | BT_LOGD_STR("Freezing clock classes."); |
| 285 | } |
| 286 | |
| 287 | for (i = 0; i < trace->clock_classes->len; i++) { |
| 288 | struct bt_ctf_clock_class *clock_class = |
| 289 | g_ptr_array_index(trace->clock_classes, i); |
| 290 | |
| 291 | bt_ctf_clock_class_freeze(clock_class); |
| 292 | } |
| 293 | |
| 294 | trace->frozen = 1; |
| 295 | } |
| 296 | |
| 297 | int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common *trace, |
| 298 | struct bt_ctf_stream_class_common *stream_class, |
| 299 | bt_ctf_validation_flag_copy_field_type_func copy_field_type_func, |
| 300 | struct bt_ctf_clock_class *init_expected_clock_class, |
| 301 | int (*map_clock_classes_func)(struct bt_ctf_stream_class_common *stream_class, |
| 302 | struct bt_ctf_field_type_common *packet_context_field_type, |
| 303 | struct bt_ctf_field_type_common *event_header_field_type), |
| 304 | bool check_ts_begin_end_mapped); |
| 305 | |
| 306 | struct bt_ctf_trace { |
| 307 | struct bt_ctf_trace_common common; |
| 308 | }; |
| 309 | |
| 310 | /* |
| 311 | * bt_ctf_trace_get_metadata_string: get metadata string. |
| 312 | * |
| 313 | * Get the trace's TSDL metadata. The caller assumes the ownership of the |
| 314 | * returned string. |
| 315 | * |
| 316 | * @param trace Trace instance. |
| 317 | * |
| 318 | * Returns the metadata string on success, NULL on error. |
| 319 | */ |
| 320 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace); |
| 321 | |
| 322 | struct bt_ctf_trace *bt_ctf_trace_create(void); |
| 323 | |
| 324 | int64_t bt_ctf_trace_get_clock_class_count( |
| 325 | struct bt_ctf_trace *trace); |
| 326 | |
| 327 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index( |
| 328 | struct bt_ctf_trace *trace, uint64_t index); |
| 329 | |
| 330 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name( |
| 331 | struct bt_ctf_trace *trace, const char *name); |
| 332 | |
| 333 | int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace, |
| 334 | struct bt_ctf_clock_class *clock_class); |
| 335 | |
| 336 | int64_t bt_ctf_trace_get_environment_field_count( |
| 337 | struct bt_ctf_trace *trace); |
| 338 | |
| 339 | const char *bt_ctf_trace_get_environment_field_name_by_index( |
| 340 | struct bt_ctf_trace *trace, uint64_t index); |
| 341 | |
| 342 | struct bt_ctf_value * |
| 343 | bt_ctf_trace_get_environment_field_value_by_index(struct bt_ctf_trace *trace, |
| 344 | uint64_t index); |
| 345 | |
| 346 | struct bt_ctf_value * |
| 347 | bt_ctf_trace_get_environment_field_value_by_name( |
| 348 | struct bt_ctf_trace *trace, const char *name); |
| 349 | |
| 350 | int bt_ctf_trace_visit(struct bt_ctf_trace *trace, |
| 351 | bt_ctf_visitor visitor, void *data); |
| 352 | |
| 353 | #endif /* BABELTRACE_CTF_WRITER_TRACE_H */ |