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