| 1 | #ifndef BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H |
| 2 | #define BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright 2014 EfficiOS Inc. |
| 6 | * |
| 7 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | * |
| 27 | * The Common Trace Format (CTF) Specification is available at |
| 28 | * http://www.efficios.com/ctf |
| 29 | */ |
| 30 | |
| 31 | #include "common/macros.h" |
| 32 | #include "common/uuid.h" |
| 33 | #include <babeltrace2-ctf-writer/field-types.h> |
| 34 | #include <babeltrace2-ctf-writer/fields.h> |
| 35 | #include <babeltrace2-ctf-writer/trace.h> |
| 36 | #include <babeltrace2/types.h> |
| 37 | #include <glib.h> |
| 38 | #include <sys/types.h> |
| 39 | |
| 40 | #include "assert-pre.h" |
| 41 | #include "attributes.h" |
| 42 | #include "clock-class.h" |
| 43 | #include "object.h" |
| 44 | #include "stream-class.h" |
| 45 | #include "validation.h" |
| 46 | #include "values.h" |
| 47 | |
| 48 | struct bt_ctf_trace_common { |
| 49 | struct bt_ctf_object base; |
| 50 | GString *name; |
| 51 | int frozen; |
| 52 | bt_uuid_t uuid; |
| 53 | bt_ctf_bool uuid_set; |
| 54 | enum bt_ctf_byte_order native_byte_order; |
| 55 | struct bt_ctf_private_value *environment; |
| 56 | GPtrArray *clock_classes; /* Array of pointers to bt_ctf_clock_class */ |
| 57 | GPtrArray *stream_classes; /* Array of ptrs to bt_ctf_stream_class_common */ |
| 58 | GPtrArray *streams; /* Array of ptrs to bt_ctf_stream_common */ |
| 59 | struct bt_ctf_field_type_common *packet_header_field_type; |
| 60 | int64_t next_stream_id; |
| 61 | |
| 62 | /* |
| 63 | * This flag indicates if the trace is valid. A valid |
| 64 | * trace is _always_ frozen. |
| 65 | */ |
| 66 | int valid; |
| 67 | }; |
| 68 | |
| 69 | BT_HIDDEN |
| 70 | bt_ctf_bool bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common *trace, |
| 71 | struct bt_ctf_clock_class *clock_class); |
| 72 | |
| 73 | BT_HIDDEN |
| 74 | int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common *trace, |
| 75 | bt_ctf_object_release_func release_func); |
| 76 | |
| 77 | BT_HIDDEN |
| 78 | void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common *trace); |
| 79 | |
| 80 | static inline |
| 81 | const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common *trace) |
| 82 | { |
| 83 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 84 | return trace->name ? trace->name->str : NULL; |
| 85 | } |
| 86 | |
| 87 | BT_HIDDEN |
| 88 | int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common *trace, const char *name); |
| 89 | |
| 90 | static inline |
| 91 | const uint8_t *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common *trace) |
| 92 | { |
| 93 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 94 | return trace->uuid_set ? trace->uuid : NULL; |
| 95 | } |
| 96 | |
| 97 | BT_HIDDEN |
| 98 | int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common *trace, const uint8_t *uuid); |
| 99 | |
| 100 | BT_HIDDEN |
| 101 | int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common *trace, |
| 102 | const char *name, struct bt_ctf_private_value *value); |
| 103 | |
| 104 | BT_HIDDEN |
| 105 | int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common *trace, |
| 106 | const char *name, const char *value); |
| 107 | |
| 108 | BT_HIDDEN |
| 109 | int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common *trace, |
| 110 | const char *name, int64_t value); |
| 111 | |
| 112 | static inline |
| 113 | int64_t bt_ctf_trace_common_get_environment_field_count( |
| 114 | struct bt_ctf_trace_common *trace) |
| 115 | { |
| 116 | int64_t ret; |
| 117 | |
| 118 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 119 | ret = bt_ctf_attributes_get_count(trace->environment); |
| 120 | BT_ASSERT_DBG(ret >= 0); |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static inline |
| 125 | const char * |
| 126 | bt_ctf_trace_common_get_environment_field_name_by_index( |
| 127 | struct bt_ctf_trace_common *trace, uint64_t index) |
| 128 | { |
| 129 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 130 | return bt_ctf_attributes_get_field_name(trace->environment, index); |
| 131 | } |
| 132 | |
| 133 | static inline |
| 134 | struct bt_ctf_private_value * |
| 135 | bt_ctf_trace_common_borrow_environment_field_value_by_index( |
| 136 | struct bt_ctf_trace_common *trace, uint64_t index) |
| 137 | { |
| 138 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 139 | return bt_ctf_attributes_borrow_field_value(trace->environment, index); |
| 140 | } |
| 141 | |
| 142 | static inline |
| 143 | struct bt_ctf_private_value * |
| 144 | bt_ctf_trace_common_borrow_environment_field_value_by_name( |
| 145 | struct bt_ctf_trace_common *trace, const char *name) |
| 146 | { |
| 147 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 148 | BT_CTF_ASSERT_PRE_NON_NULL(name, "Name"); |
| 149 | return bt_ctf_attributes_borrow_field_value_by_name(trace->environment, |
| 150 | name); |
| 151 | } |
| 152 | |
| 153 | BT_HIDDEN |
| 154 | int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common *trace, |
| 155 | struct bt_ctf_clock_class *clock_class); |
| 156 | |
| 157 | static inline |
| 158 | int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common *trace) |
| 159 | { |
| 160 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 161 | return trace->clock_classes->len; |
| 162 | } |
| 163 | |
| 164 | static inline |
| 165 | struct bt_ctf_clock_class *bt_ctf_trace_common_borrow_clock_class_by_index( |
| 166 | struct bt_ctf_trace_common *trace, uint64_t index) |
| 167 | { |
| 168 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 169 | BT_CTF_ASSERT_PRE(index < trace->clock_classes->len, |
| 170 | "Index is out of bounds: index=%" PRIu64 ", " |
| 171 | "count=%u", |
| 172 | index, trace->clock_classes->len); |
| 173 | return g_ptr_array_index(trace->clock_classes, index); |
| 174 | } |
| 175 | |
| 176 | static inline |
| 177 | int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common *trace) |
| 178 | { |
| 179 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 180 | return (int64_t) trace->streams->len; |
| 181 | } |
| 182 | |
| 183 | static inline |
| 184 | struct bt_ctf_stream_common *bt_ctf_trace_common_borrow_stream_by_index( |
| 185 | struct bt_ctf_trace_common *trace, |
| 186 | uint64_t index) |
| 187 | { |
| 188 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 189 | BT_CTF_ASSERT_PRE(index < trace->streams->len, |
| 190 | "Index is out of bounds: index=%" PRIu64 ", " |
| 191 | "count=%u", |
| 192 | index, trace->streams->len); |
| 193 | return g_ptr_array_index(trace->streams, index); |
| 194 | } |
| 195 | |
| 196 | static inline |
| 197 | int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common *trace) |
| 198 | { |
| 199 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 200 | return (int64_t) trace->stream_classes->len; |
| 201 | } |
| 202 | |
| 203 | static inline |
| 204 | struct bt_ctf_stream_class_common *bt_ctf_trace_common_borrow_stream_class_by_index( |
| 205 | struct bt_ctf_trace_common *trace, uint64_t index) |
| 206 | { |
| 207 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 208 | BT_CTF_ASSERT_PRE(index < trace->stream_classes->len, |
| 209 | "Index is out of bounds: index=%" PRIu64 ", " |
| 210 | "count=%u", |
| 211 | index, trace->stream_classes->len); |
| 212 | return g_ptr_array_index(trace->stream_classes, index); |
| 213 | } |
| 214 | |
| 215 | static inline |
| 216 | struct bt_ctf_stream_class_common *bt_ctf_trace_common_borrow_stream_class_by_id( |
| 217 | struct bt_ctf_trace_common *trace, uint64_t id_param) |
| 218 | { |
| 219 | int i; |
| 220 | struct bt_ctf_stream_class_common *stream_class = NULL; |
| 221 | int64_t id = (int64_t) id_param; |
| 222 | |
| 223 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 224 | BT_CTF_ASSERT_PRE(id >= 0, |
| 225 | "Invalid stream class ID: %" PRIu64, id_param); |
| 226 | |
| 227 | for (i = 0; i < trace->stream_classes->len; i++) { |
| 228 | struct bt_ctf_stream_class_common *stream_class_candidate; |
| 229 | |
| 230 | stream_class_candidate = |
| 231 | g_ptr_array_index(trace->stream_classes, i); |
| 232 | |
| 233 | if (bt_ctf_stream_class_common_get_id(stream_class_candidate) == |
| 234 | (int64_t) id) { |
| 235 | stream_class = stream_class_candidate; |
| 236 | goto end; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | end: |
| 241 | return stream_class; |
| 242 | } |
| 243 | |
| 244 | static inline |
| 245 | struct bt_ctf_clock_class *bt_ctf_trace_common_borrow_clock_class_by_name( |
| 246 | struct bt_ctf_trace_common *trace, const char *name) |
| 247 | { |
| 248 | size_t i; |
| 249 | struct bt_ctf_clock_class *clock_class = NULL; |
| 250 | |
| 251 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 252 | BT_CTF_ASSERT_PRE_NON_NULL(name, "Name"); |
| 253 | |
| 254 | for (i = 0; i < trace->clock_classes->len; i++) { |
| 255 | struct bt_ctf_clock_class *cur_clk = |
| 256 | g_ptr_array_index(trace->clock_classes, i); |
| 257 | const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk); |
| 258 | |
| 259 | if (!cur_clk_name) { |
| 260 | goto end; |
| 261 | } |
| 262 | |
| 263 | if (!strcmp(cur_clk_name, name)) { |
| 264 | clock_class = cur_clk; |
| 265 | goto end; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | end: |
| 270 | return clock_class; |
| 271 | } |
| 272 | |
| 273 | static inline |
| 274 | enum bt_ctf_byte_order bt_ctf_trace_common_get_native_byte_order( |
| 275 | struct bt_ctf_trace_common *trace) |
| 276 | { |
| 277 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 278 | return trace->native_byte_order; |
| 279 | } |
| 280 | |
| 281 | BT_HIDDEN |
| 282 | int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common *trace, |
| 283 | enum bt_ctf_byte_order byte_order, bool allow_unspecified); |
| 284 | |
| 285 | static inline |
| 286 | struct bt_ctf_field_type_common *bt_ctf_trace_common_borrow_packet_header_field_type( |
| 287 | struct bt_ctf_trace_common *trace) |
| 288 | { |
| 289 | BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace"); |
| 290 | return trace->packet_header_field_type; |
| 291 | } |
| 292 | |
| 293 | BT_HIDDEN |
| 294 | int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common *trace, |
| 295 | struct bt_ctf_field_type_common *packet_header_field_type); |
| 296 | |
| 297 | static inline |
| 298 | void bt_ctf_trace_common_freeze(struct bt_ctf_trace_common *trace) |
| 299 | { |
| 300 | int i; |
| 301 | |
| 302 | if (trace->frozen) { |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | BT_LOGD("Freezing trace: addr=%p, name=\"%s\"", |
| 307 | trace, bt_ctf_trace_common_get_name(trace)); |
| 308 | BT_LOGD_STR("Freezing packet header field type."); |
| 309 | bt_ctf_field_type_common_freeze(trace->packet_header_field_type); |
| 310 | BT_LOGD_STR("Freezing environment attributes."); |
| 311 | bt_ctf_attributes_freeze(trace->environment); |
| 312 | |
| 313 | if (trace->clock_classes->len > 0) { |
| 314 | BT_LOGD_STR("Freezing clock classes."); |
| 315 | } |
| 316 | |
| 317 | for (i = 0; i < trace->clock_classes->len; i++) { |
| 318 | struct bt_ctf_clock_class *clock_class = |
| 319 | g_ptr_array_index(trace->clock_classes, i); |
| 320 | |
| 321 | bt_ctf_clock_class_freeze(clock_class); |
| 322 | } |
| 323 | |
| 324 | trace->frozen = 1; |
| 325 | } |
| 326 | |
| 327 | BT_HIDDEN |
| 328 | int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common *trace, |
| 329 | struct bt_ctf_stream_class_common *stream_class, |
| 330 | bt_ctf_validation_flag_copy_field_type_func copy_field_type_func, |
| 331 | struct bt_ctf_clock_class *init_expected_clock_class, |
| 332 | int (*map_clock_classes_func)(struct bt_ctf_stream_class_common *stream_class, |
| 333 | struct bt_ctf_field_type_common *packet_context_field_type, |
| 334 | struct bt_ctf_field_type_common *event_header_field_type), |
| 335 | bool check_ts_begin_end_mapped); |
| 336 | |
| 337 | struct bt_ctf_trace { |
| 338 | struct bt_ctf_trace_common common; |
| 339 | }; |
| 340 | |
| 341 | /* |
| 342 | * bt_ctf_trace_get_metadata_string: get metadata string. |
| 343 | * |
| 344 | * Get the trace's TSDL metadata. The caller assumes the ownership of the |
| 345 | * returned string. |
| 346 | * |
| 347 | * @param trace Trace instance. |
| 348 | * |
| 349 | * Returns the metadata string on success, NULL on error. |
| 350 | */ |
| 351 | BT_HIDDEN |
| 352 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace); |
| 353 | |
| 354 | BT_HIDDEN |
| 355 | struct bt_ctf_trace *bt_ctf_trace_create(void); |
| 356 | |
| 357 | BT_HIDDEN |
| 358 | int64_t bt_ctf_trace_get_clock_class_count( |
| 359 | struct bt_ctf_trace *trace); |
| 360 | |
| 361 | BT_HIDDEN |
| 362 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index( |
| 363 | struct bt_ctf_trace *trace, uint64_t index); |
| 364 | |
| 365 | BT_HIDDEN |
| 366 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name( |
| 367 | struct bt_ctf_trace *trace, const char *name); |
| 368 | |
| 369 | BT_HIDDEN |
| 370 | int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace, |
| 371 | struct bt_ctf_clock_class *clock_class); |
| 372 | |
| 373 | BT_HIDDEN |
| 374 | int64_t bt_ctf_trace_get_environment_field_count( |
| 375 | struct bt_ctf_trace *trace); |
| 376 | |
| 377 | BT_HIDDEN |
| 378 | const char *bt_ctf_trace_get_environment_field_name_by_index( |
| 379 | struct bt_ctf_trace *trace, uint64_t index); |
| 380 | |
| 381 | BT_HIDDEN |
| 382 | struct bt_ctf_value * |
| 383 | bt_ctf_trace_get_environment_field_value_by_index(struct bt_ctf_trace *trace, |
| 384 | uint64_t index); |
| 385 | |
| 386 | BT_HIDDEN |
| 387 | struct bt_ctf_value * |
| 388 | bt_ctf_trace_get_environment_field_value_by_name( |
| 389 | struct bt_ctf_trace *trace, const char *name); |
| 390 | |
| 391 | BT_HIDDEN |
| 392 | int bt_ctf_trace_visit(struct bt_ctf_trace *trace, |
| 393 | bt_ctf_visitor visitor, void *data); |
| 394 | |
| 395 | #endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */ |