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