Commit | Line | Data |
---|---|---|
bc37ae52 JG |
1 | /* |
2 | * trace.c | |
3 | * | |
4 | * Babeltrace CTF IR - Trace | |
5 | * | |
6 | * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
e011d2c1 PP |
29 | #define BT_LOG_TAG "TRACE" |
30 | #include <babeltrace/lib-logging-internal.h> | |
31 | ||
bc37ae52 | 32 | #include <babeltrace/ctf-ir/trace-internal.h> |
ac0c6bdd | 33 | #include <babeltrace/ctf-ir/clock-class-internal.h> |
bc37ae52 JG |
34 | #include <babeltrace/ctf-ir/stream-internal.h> |
35 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
09840de5 | 36 | #include <babeltrace/ctf-ir/event-internal.h> |
272df73e PP |
37 | #include <babeltrace/ctf-ir/event-class.h> |
38 | #include <babeltrace/ctf-ir/event-class-internal.h> | |
bc37ae52 | 39 | #include <babeltrace/ctf-writer/functor-internal.h> |
ac0c6bdd | 40 | #include <babeltrace/ctf-writer/clock-internal.h> |
2e33ac5a | 41 | #include <babeltrace/ctf-ir/field-types-internal.h> |
44e0a4f5 | 42 | #include <babeltrace/ctf-ir/attributes-internal.h> |
09840de5 | 43 | #include <babeltrace/ctf-ir/validation-internal.h> |
8bf65fbd | 44 | #include <babeltrace/ctf-ir/visitor-internal.h> |
654c1444 | 45 | #include <babeltrace/ctf-ir/utils.h> |
726106d3 | 46 | #include <babeltrace/ctf-ir/utils-internal.h> |
3d9990ac | 47 | #include <babeltrace/compiler-internal.h> |
dac5c838 | 48 | #include <babeltrace/values.h> |
95c09b3a | 49 | #include <babeltrace/values-internal.h> |
83509119 | 50 | #include <babeltrace/ref.h> |
c55a9f58 | 51 | #include <babeltrace/types.h> |
3d9990ac | 52 | #include <babeltrace/endian-internal.h> |
dc3fffef | 53 | #include <inttypes.h> |
544d0515 | 54 | #include <stdint.h> |
4a32fda0 | 55 | #include <string.h> |
0fbb9a9f | 56 | #include <stdlib.h> |
bc37ae52 JG |
57 | |
58 | #define DEFAULT_IDENTIFIER_SIZE 128 | |
59 | #define DEFAULT_METADATA_STRING_SIZE 4096 | |
60 | ||
50ad4244 | 61 | struct listener_wrapper { |
50842bdc | 62 | bt_listener_cb listener; |
2204c381 JG |
63 | void *data; |
64 | }; | |
65 | ||
50842bdc PP |
66 | struct bt_trace_is_static_listener_elem { |
67 | bt_trace_is_static_listener func; | |
68 | bt_trace_listener_removed removed; | |
3602afb0 PP |
69 | void *data; |
70 | }; | |
71 | ||
bc37ae52 | 72 | static |
50842bdc | 73 | void bt_trace_destroy(struct bt_object *obj); |
bc37ae52 | 74 | static |
50842bdc | 75 | void bt_trace_freeze(struct bt_trace *trace); |
bc37ae52 | 76 | |
bc37ae52 JG |
77 | static |
78 | const unsigned int field_type_aliases_alignments[] = { | |
79 | [FIELD_TYPE_ALIAS_UINT5_T] = 1, | |
80 | [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, | |
81 | [FIELD_TYPE_ALIAS_UINT27_T] = 1, | |
82 | [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, | |
83 | }; | |
84 | ||
85 | static | |
86 | const unsigned int field_type_aliases_sizes[] = { | |
87 | [FIELD_TYPE_ALIAS_UINT5_T] = 5, | |
88 | [FIELD_TYPE_ALIAS_UINT8_T] = 8, | |
89 | [FIELD_TYPE_ALIAS_UINT16_T] = 16, | |
90 | [FIELD_TYPE_ALIAS_UINT27_T] = 27, | |
91 | [FIELD_TYPE_ALIAS_UINT32_T] = 32, | |
92 | [FIELD_TYPE_ALIAS_UINT64_T] = 64, | |
93 | }; | |
94 | ||
50842bdc | 95 | struct bt_trace *bt_trace_create(void) |
bc37ae52 | 96 | { |
50842bdc | 97 | struct bt_trace *trace = NULL; |
bc37ae52 | 98 | |
50842bdc | 99 | trace = g_new0(struct bt_trace, 1); |
bc37ae52 | 100 | if (!trace) { |
95c09b3a | 101 | BT_LOGE_STR("Failed to allocate one trace."); |
bc37ae52 JG |
102 | goto error; |
103 | } | |
104 | ||
95c09b3a | 105 | BT_LOGD_STR("Creating trace object."); |
50842bdc PP |
106 | trace->native_byte_order = BT_BYTE_ORDER_UNSPECIFIED; |
107 | bt_object_init(trace, bt_trace_destroy); | |
bc37ae52 | 108 | trace->clocks = g_ptr_array_new_with_free_func( |
83509119 | 109 | (GDestroyNotify) bt_put); |
95c09b3a PP |
110 | if (!trace->clocks) { |
111 | BT_LOGE_STR("Failed to allocate one GPtrArray."); | |
112 | goto error; | |
113 | } | |
114 | ||
bc37ae52 | 115 | trace->streams = g_ptr_array_new_with_free_func( |
e6a8e8e4 | 116 | (GDestroyNotify) bt_object_release); |
95c09b3a PP |
117 | if (!trace->streams) { |
118 | BT_LOGE_STR("Failed to allocate one GPtrArray."); | |
119 | goto error; | |
120 | } | |
121 | ||
bc37ae52 | 122 | trace->stream_classes = g_ptr_array_new_with_free_func( |
e6a8e8e4 | 123 | (GDestroyNotify) bt_object_release); |
95c09b3a PP |
124 | if (!trace->stream_classes) { |
125 | BT_LOGE_STR("Failed to allocate one GPtrArray."); | |
83509119 | 126 | goto error; |
bc37ae52 JG |
127 | } |
128 | ||
7f800dc7 | 129 | /* Create the environment array object */ |
50842bdc | 130 | trace->environment = bt_attributes_create(); |
7f800dc7 | 131 | if (!trace->environment) { |
95c09b3a | 132 | BT_LOGE_STR("Cannot create empty attributes object."); |
83509119 | 133 | goto error; |
7f800dc7 PP |
134 | } |
135 | ||
9b888ff3 JG |
136 | trace->listeners = g_ptr_array_new_with_free_func( |
137 | (GDestroyNotify) g_free); | |
138 | if (!trace->listeners) { | |
95c09b3a | 139 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
9b888ff3 JG |
140 | goto error; |
141 | } | |
142 | ||
3602afb0 | 143 | trace->is_static_listeners = g_array_new(FALSE, TRUE, |
50842bdc | 144 | sizeof(struct bt_trace_is_static_listener_elem)); |
3602afb0 PP |
145 | if (!trace->is_static_listeners) { |
146 | BT_LOGE_STR("Failed to allocate one GArray."); | |
147 | goto error; | |
148 | } | |
149 | ||
95c09b3a | 150 | BT_LOGD("Created trace object: addr=%p", trace); |
bc37ae52 JG |
151 | return trace; |
152 | ||
bc37ae52 | 153 | error: |
83509119 | 154 | BT_PUT(trace); |
bc37ae52 JG |
155 | return trace; |
156 | } | |
157 | ||
50842bdc | 158 | const char *bt_trace_get_name(struct bt_trace *trace) |
e96045d4 JG |
159 | { |
160 | const char *name = NULL; | |
161 | ||
95c09b3a PP |
162 | if (!trace) { |
163 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
164 | goto end; | |
165 | } | |
166 | ||
167 | if (!trace->name) { | |
e96045d4 JG |
168 | goto end; |
169 | } | |
170 | ||
171 | name = trace->name->str; | |
172 | end: | |
173 | return name; | |
174 | } | |
175 | ||
50842bdc | 176 | int bt_trace_set_name(struct bt_trace *trace, const char *name) |
e96045d4 JG |
177 | { |
178 | int ret = 0; | |
179 | ||
95c09b3a PP |
180 | if (!trace) { |
181 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
182 | ret = -1; | |
183 | goto end; | |
184 | } | |
185 | ||
186 | if (!name) { | |
187 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
188 | ret = -1; | |
189 | goto end; | |
190 | } | |
191 | ||
192 | if (trace->frozen) { | |
193 | BT_LOGW("Invalid parameter: trace is frozen: " | |
194 | "addr=%p, name=\"%s\"", | |
50842bdc | 195 | trace, bt_trace_get_name(trace)); |
e96045d4 JG |
196 | ret = -1; |
197 | goto end; | |
198 | } | |
199 | ||
200 | trace->name = trace->name ? g_string_assign(trace->name, name) : | |
201 | g_string_new(name); | |
202 | if (!trace->name) { | |
95c09b3a | 203 | BT_LOGE_STR("Failed to allocate one GString."); |
e96045d4 JG |
204 | ret = -1; |
205 | goto end; | |
206 | } | |
95c09b3a PP |
207 | |
208 | BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace, name); | |
209 | ||
e96045d4 JG |
210 | end: |
211 | return ret; | |
212 | } | |
213 | ||
50842bdc | 214 | const unsigned char *bt_trace_get_uuid(struct bt_trace *trace) |
4a32fda0 PP |
215 | { |
216 | return trace && trace->uuid_set ? trace->uuid : NULL; | |
217 | } | |
218 | ||
50842bdc | 219 | int bt_trace_set_uuid(struct bt_trace *trace, const unsigned char *uuid) |
4a32fda0 PP |
220 | { |
221 | int ret = 0; | |
222 | ||
95c09b3a PP |
223 | if (!trace) { |
224 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
225 | ret = -1; | |
226 | goto end; | |
227 | } | |
228 | ||
229 | if (!uuid) { | |
230 | BT_LOGW_STR("Invalid parameter: UUID is NULL."); | |
231 | ret = -1; | |
232 | goto end; | |
233 | } | |
234 | ||
235 | if (trace->frozen) { | |
236 | BT_LOGW("Invalid parameter: trace is frozen: " | |
237 | "addr=%p, name=\"%s\"", | |
50842bdc | 238 | trace, bt_trace_get_name(trace)); |
4a32fda0 PP |
239 | ret = -1; |
240 | goto end; | |
241 | } | |
242 | ||
20eee76e | 243 | memcpy(trace->uuid, uuid, BABELTRACE_UUID_LEN); |
c55a9f58 | 244 | trace->uuid_set = BT_TRUE; |
95c09b3a PP |
245 | BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", " |
246 | "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", | |
50842bdc | 247 | trace, bt_trace_get_name(trace), |
95c09b3a PP |
248 | (unsigned int) uuid[0], |
249 | (unsigned int) uuid[1], | |
250 | (unsigned int) uuid[2], | |
251 | (unsigned int) uuid[3], | |
252 | (unsigned int) uuid[4], | |
253 | (unsigned int) uuid[5], | |
254 | (unsigned int) uuid[6], | |
255 | (unsigned int) uuid[7], | |
256 | (unsigned int) uuid[8], | |
257 | (unsigned int) uuid[9], | |
258 | (unsigned int) uuid[10], | |
259 | (unsigned int) uuid[11], | |
260 | (unsigned int) uuid[12], | |
261 | (unsigned int) uuid[13], | |
262 | (unsigned int) uuid[14], | |
263 | (unsigned int) uuid[15]); | |
4a32fda0 PP |
264 | |
265 | end: | |
266 | return ret; | |
267 | } | |
268 | ||
50842bdc | 269 | void bt_trace_destroy(struct bt_object *obj) |
bc37ae52 | 270 | { |
50842bdc | 271 | struct bt_trace *trace; |
bc37ae52 | 272 | |
50842bdc | 273 | trace = container_of(obj, struct bt_trace, base); |
95c09b3a PP |
274 | |
275 | BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"", | |
50842bdc | 276 | trace, bt_trace_get_name(trace)); |
95c09b3a | 277 | |
8480c8cc PP |
278 | /* |
279 | * Call remove listeners first so that everything else still | |
280 | * exists in the trace. | |
281 | */ | |
282 | if (trace->is_static_listeners) { | |
283 | size_t i; | |
284 | ||
285 | for (i = 0; i < trace->is_static_listeners->len; i++) { | |
50842bdc | 286 | struct bt_trace_is_static_listener_elem elem = |
8480c8cc | 287 | g_array_index(trace->is_static_listeners, |
50842bdc | 288 | struct bt_trace_is_static_listener_elem, i); |
8480c8cc PP |
289 | |
290 | if (elem.removed) { | |
291 | elem.removed(trace, elem.data); | |
292 | } | |
293 | } | |
294 | ||
295 | g_array_free(trace->is_static_listeners, TRUE); | |
296 | } | |
297 | ||
298 | if (trace->listeners) { | |
299 | g_ptr_array_free(trace->listeners, TRUE); | |
300 | } | |
301 | ||
bc37ae52 | 302 | if (trace->environment) { |
95c09b3a | 303 | BT_LOGD_STR("Destroying environment attributes."); |
50842bdc | 304 | bt_attributes_destroy(trace->environment); |
bc37ae52 JG |
305 | } |
306 | ||
e96045d4 JG |
307 | if (trace->name) { |
308 | g_string_free(trace->name, TRUE); | |
309 | } | |
310 | ||
bc37ae52 | 311 | if (trace->clocks) { |
95c09b3a | 312 | BT_LOGD_STR("Putting clock classes."); |
bc37ae52 JG |
313 | g_ptr_array_free(trace->clocks, TRUE); |
314 | } | |
315 | ||
316 | if (trace->streams) { | |
607ff9b8 | 317 | BT_LOGD_STR("Destroying streams."); |
bc37ae52 JG |
318 | g_ptr_array_free(trace->streams, TRUE); |
319 | } | |
320 | ||
321 | if (trace->stream_classes) { | |
607ff9b8 | 322 | BT_LOGD_STR("Destroying stream classes."); |
bc37ae52 JG |
323 | g_ptr_array_free(trace->stream_classes, TRUE); |
324 | } | |
325 | ||
95c09b3a | 326 | BT_LOGD_STR("Putting packet header field type."); |
83509119 | 327 | bt_put(trace->packet_header_type); |
bc37ae52 JG |
328 | g_free(trace); |
329 | } | |
330 | ||
50842bdc | 331 | int bt_trace_set_environment_field(struct bt_trace *trace, |
dac5c838 | 332 | const char *name, struct bt_value *value) |
bc37ae52 | 333 | { |
bc37ae52 JG |
334 | int ret = 0; |
335 | ||
95c09b3a PP |
336 | if (!trace) { |
337 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
338 | ret = -1; | |
339 | goto end; | |
340 | } | |
341 | ||
342 | if (!name) { | |
343 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
344 | ret = -1; | |
345 | goto end; | |
346 | } | |
347 | ||
348 | if (!value) { | |
349 | BT_LOGW_STR("Invalid parameter: value is NULL."); | |
350 | ret = -1; | |
351 | goto end; | |
352 | } | |
353 | ||
f4cc9a21 | 354 | if (!bt_identifier_is_valid(name)) { |
95c09b3a PP |
355 | BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: " |
356 | "trace-addr=%p, trace-name=\"%s\", " | |
357 | "env-name=\"%s\"", | |
50842bdc | 358 | trace, bt_trace_get_name(trace), name); |
bc37ae52 | 359 | ret = -1; |
7f800dc7 | 360 | goto end; |
bc37ae52 JG |
361 | } |
362 | ||
95c09b3a PP |
363 | if (!bt_value_is_integer(value) && !bt_value_is_string(value)) { |
364 | BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: " | |
365 | "trace-addr=%p, trace-name=\"%s\", " | |
366 | "env-name=\"%s\", env-value-type=%s", | |
50842bdc | 367 | trace, bt_trace_get_name(trace), name, |
95c09b3a PP |
368 | bt_value_type_string(bt_value_get_type(value))); |
369 | ret = -1; | |
370 | goto end; | |
371 | } | |
372 | ||
373 | if (trace->is_static) { | |
374 | BT_LOGW("Invalid parameter: trace is static: " | |
375 | "addr=%p, name=\"%s\"", | |
50842bdc | 376 | trace, bt_trace_get_name(trace)); |
bc37ae52 | 377 | ret = -1; |
7f800dc7 | 378 | goto end; |
bc37ae52 JG |
379 | } |
380 | ||
a0d12916 JG |
381 | if (trace->frozen) { |
382 | /* | |
383 | * New environment fields may be added to a frozen trace, | |
384 | * but existing fields may not be changed. | |
385 | * | |
386 | * The object passed is frozen like all other attributes. | |
387 | */ | |
dac5c838 | 388 | struct bt_value *attribute = |
50842bdc | 389 | bt_attributes_get_field_value_by_name( |
a0d12916 JG |
390 | trace->environment, name); |
391 | ||
392 | if (attribute) { | |
95c09b3a PP |
393 | BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: " |
394 | "trace-addr=%p, trace-name=\"%s\", " | |
395 | "env-name=\"%s\"", | |
50842bdc | 396 | trace, bt_trace_get_name(trace), name); |
83509119 | 397 | BT_PUT(attribute); |
a0d12916 JG |
398 | ret = -1; |
399 | goto end; | |
400 | } | |
401 | ||
dac5c838 | 402 | bt_value_freeze(value); |
a0d12916 JG |
403 | } |
404 | ||
50842bdc | 405 | ret = bt_attributes_set_field_value(trace->environment, name, |
7f800dc7 | 406 | value); |
95c09b3a PP |
407 | if (ret) { |
408 | BT_LOGE("Cannot set environment field's value: " | |
409 | "trace-addr=%p, trace-name=\"%s\", " | |
410 | "env-name=\"%s\"", | |
50842bdc | 411 | trace, bt_trace_get_name(trace), name); |
95c09b3a PP |
412 | } else { |
413 | BT_LOGV("Set environment field's value: " | |
414 | "trace-addr=%p, trace-name=\"%s\", " | |
415 | "env-name=\"%s\", value-addr=%p", | |
50842bdc | 416 | trace, bt_trace_get_name(trace), name, value); |
95c09b3a | 417 | } |
bc37ae52 | 418 | |
7f800dc7 PP |
419 | end: |
420 | return ret; | |
421 | } | |
bc37ae52 | 422 | |
50842bdc | 423 | int bt_trace_set_environment_field_string(struct bt_trace *trace, |
7f800dc7 PP |
424 | const char *name, const char *value) |
425 | { | |
426 | int ret = 0; | |
dac5c838 | 427 | struct bt_value *env_value_string_obj = NULL; |
7f800dc7 | 428 | |
95c09b3a PP |
429 | if (!value) { |
430 | BT_LOGW_STR("Invalid parameter: value is NULL."); | |
bc37ae52 | 431 | ret = -1; |
7f800dc7 | 432 | goto end; |
bc37ae52 JG |
433 | } |
434 | ||
dac5c838 | 435 | env_value_string_obj = bt_value_string_create_init(value); |
7f800dc7 | 436 | if (!env_value_string_obj) { |
95c09b3a | 437 | BT_LOGE_STR("Cannot create string value object."); |
7f800dc7 PP |
438 | ret = -1; |
439 | goto end; | |
bc37ae52 JG |
440 | } |
441 | ||
50842bdc PP |
442 | /* bt_trace_set_environment_field() logs errors */ |
443 | ret = bt_trace_set_environment_field(trace, name, | |
7f800dc7 PP |
444 | env_value_string_obj); |
445 | ||
446 | end: | |
95c09b3a | 447 | bt_put(env_value_string_obj); |
3487c9f3 JG |
448 | return ret; |
449 | } | |
450 | ||
50842bdc | 451 | int bt_trace_set_environment_field_integer(struct bt_trace *trace, |
7f800dc7 | 452 | const char *name, int64_t value) |
3487c9f3 | 453 | { |
3487c9f3 | 454 | int ret = 0; |
dac5c838 | 455 | struct bt_value *env_value_integer_obj = NULL; |
3487c9f3 | 456 | |
dac5c838 | 457 | env_value_integer_obj = bt_value_integer_create_init(value); |
7f800dc7 | 458 | if (!env_value_integer_obj) { |
95c09b3a | 459 | BT_LOGE_STR("Cannot create integer value object."); |
3487c9f3 | 460 | ret = -1; |
7f800dc7 | 461 | goto end; |
3487c9f3 JG |
462 | } |
463 | ||
50842bdc PP |
464 | /* bt_trace_set_environment_field() logs errors */ |
465 | ret = bt_trace_set_environment_field(trace, name, | |
7f800dc7 | 466 | env_value_integer_obj); |
95c09b3a | 467 | |
7f800dc7 | 468 | end: |
95c09b3a | 469 | bt_put(env_value_integer_obj); |
bc37ae52 JG |
470 | return ret; |
471 | } | |
472 | ||
50842bdc | 473 | int64_t bt_trace_get_environment_field_count(struct bt_trace *trace) |
e6fa2160 | 474 | { |
544d0515 | 475 | int64_t ret = 0; |
e6fa2160 JG |
476 | |
477 | if (!trace) { | |
95c09b3a | 478 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
9ac68eb1 | 479 | ret = (int64_t) -1; |
e6fa2160 JG |
480 | goto end; |
481 | } | |
482 | ||
50842bdc | 483 | ret = bt_attributes_get_count(trace->environment); |
95c09b3a | 484 | assert(ret >= 0); |
e6fa2160 | 485 | |
e6fa2160 | 486 | end: |
7f800dc7 | 487 | return ret; |
e6fa2160 JG |
488 | } |
489 | ||
490 | const char * | |
50842bdc | 491 | bt_trace_get_environment_field_name_by_index(struct bt_trace *trace, |
9ac68eb1 | 492 | uint64_t index) |
e6fa2160 | 493 | { |
e6fa2160 JG |
494 | const char *ret = NULL; |
495 | ||
7f800dc7 | 496 | if (!trace) { |
95c09b3a | 497 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
e6fa2160 JG |
498 | goto end; |
499 | } | |
500 | ||
50842bdc | 501 | ret = bt_attributes_get_field_name(trace->environment, index); |
7f800dc7 | 502 | |
e6fa2160 JG |
503 | end: |
504 | return ret; | |
505 | } | |
506 | ||
50842bdc PP |
507 | struct bt_value *bt_trace_get_environment_field_value_by_index( |
508 | struct bt_trace *trace, uint64_t index) | |
e6fa2160 | 509 | { |
dac5c838 | 510 | struct bt_value *ret = NULL; |
e6fa2160 | 511 | |
7f800dc7 | 512 | if (!trace) { |
95c09b3a | 513 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
e6fa2160 JG |
514 | goto end; |
515 | } | |
516 | ||
50842bdc | 517 | ret = bt_attributes_get_field_value(trace->environment, index); |
7f800dc7 | 518 | |
e6fa2160 JG |
519 | end: |
520 | return ret; | |
521 | } | |
522 | ||
50842bdc PP |
523 | struct bt_value *bt_trace_get_environment_field_value_by_name( |
524 | struct bt_trace *trace, const char *name) | |
e6fa2160 | 525 | { |
dac5c838 | 526 | struct bt_value *ret = NULL; |
e6fa2160 | 527 | |
95c09b3a PP |
528 | if (!trace) { |
529 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
530 | goto end; | |
531 | } | |
532 | ||
533 | if (!name) { | |
534 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
e6fa2160 JG |
535 | goto end; |
536 | } | |
537 | ||
50842bdc | 538 | ret = bt_attributes_get_field_value_by_name(trace->environment, |
7f800dc7 PP |
539 | name); |
540 | ||
e6fa2160 JG |
541 | end: |
542 | return ret; | |
543 | } | |
544 | ||
50842bdc PP |
545 | int bt_trace_add_clock_class(struct bt_trace *trace, |
546 | struct bt_clock_class *clock_class) | |
bc37ae52 JG |
547 | { |
548 | int ret = 0; | |
bc37ae52 | 549 | |
95c09b3a PP |
550 | if (!trace) { |
551 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
552 | ret = -1; | |
553 | goto end; | |
554 | } | |
555 | ||
556 | if (trace->is_static) { | |
557 | BT_LOGW("Invalid parameter: trace is static: " | |
558 | "addr=%p, name=\"%s\"", | |
50842bdc | 559 | trace, bt_trace_get_name(trace)); |
95c09b3a PP |
560 | ret = -1; |
561 | goto end; | |
562 | } | |
563 | ||
50842bdc | 564 | if (!bt_clock_class_is_valid(clock_class)) { |
95c09b3a PP |
565 | BT_LOGW("Invalid parameter: clock class is invalid: " |
566 | "trace-addr=%p, trace-name=\"%s\", " | |
567 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
50842bdc PP |
568 | trace, bt_trace_get_name(trace), |
569 | clock_class, bt_clock_class_get_name(clock_class)); | |
bc37ae52 JG |
570 | ret = -1; |
571 | goto end; | |
572 | } | |
573 | ||
ac0c6bdd | 574 | /* Check for duplicate clock classes */ |
50842bdc | 575 | if (bt_trace_has_clock_class(trace, clock_class)) { |
95c09b3a PP |
576 | BT_LOGW("Invalid parameter: clock class already exists in trace: " |
577 | "trace-addr=%p, trace-name=\"%s\", " | |
578 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
50842bdc PP |
579 | trace, bt_trace_get_name(trace), |
580 | clock_class, bt_clock_class_get_name(clock_class)); | |
bc37ae52 JG |
581 | ret = -1; |
582 | goto end; | |
583 | } | |
584 | ||
ac0c6bdd PP |
585 | bt_get(clock_class); |
586 | g_ptr_array_add(trace->clocks, clock_class); | |
cfeb617e | 587 | |
6474e016 | 588 | if (trace->frozen) { |
95c09b3a | 589 | BT_LOGV_STR("Freezing added clock class because trace is frozen."); |
50842bdc | 590 | bt_clock_class_freeze(clock_class); |
6474e016 | 591 | } |
95c09b3a PP |
592 | |
593 | BT_LOGV("Added clock class to trace: " | |
594 | "trace-addr=%p, trace-name=\"%s\", " | |
595 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
50842bdc PP |
596 | trace, bt_trace_get_name(trace), |
597 | clock_class, bt_clock_class_get_name(clock_class)); | |
95c09b3a | 598 | |
bc37ae52 JG |
599 | end: |
600 | return ret; | |
601 | } | |
602 | ||
50842bdc | 603 | int64_t bt_trace_get_clock_class_count(struct bt_trace *trace) |
884cd6c3 | 604 | { |
9ac68eb1 | 605 | int64_t ret = (int64_t) -1; |
884cd6c3 JG |
606 | |
607 | if (!trace) { | |
95c09b3a | 608 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
884cd6c3 JG |
609 | goto end; |
610 | } | |
611 | ||
612 | ret = trace->clocks->len; | |
613 | end: | |
614 | return ret; | |
615 | } | |
616 | ||
50842bdc PP |
617 | struct bt_clock_class *bt_trace_get_clock_class_by_index( |
618 | struct bt_trace *trace, uint64_t index) | |
884cd6c3 | 619 | { |
50842bdc | 620 | struct bt_clock_class *clock_class = NULL; |
884cd6c3 | 621 | |
95c09b3a PP |
622 | if (!trace) { |
623 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
624 | goto end; | |
625 | } | |
626 | ||
627 | if (index >= trace->clocks->len) { | |
628 | BT_LOGW("Invalid parameter: index is out of bounds: " | |
629 | "addr=%p, name=\"%s\", " | |
630 | "index=%" PRIu64 ", count=%u", | |
50842bdc | 631 | trace, bt_trace_get_name(trace), |
95c09b3a | 632 | index, trace->clocks->len); |
884cd6c3 JG |
633 | goto end; |
634 | } | |
635 | ||
ac0c6bdd PP |
636 | clock_class = g_ptr_array_index(trace->clocks, index); |
637 | bt_get(clock_class); | |
884cd6c3 | 638 | end: |
ac0c6bdd | 639 | return clock_class; |
884cd6c3 JG |
640 | } |
641 | ||
e011d2c1 | 642 | static |
50842bdc PP |
643 | bool packet_header_field_type_is_valid(struct bt_trace *trace, |
644 | struct bt_field_type *packet_header_type) | |
e011d2c1 PP |
645 | { |
646 | int ret; | |
647 | bool is_valid = true; | |
50842bdc | 648 | struct bt_field_type *field_type = NULL; |
e011d2c1 PP |
649 | |
650 | if (!packet_header_type) { | |
651 | /* | |
652 | * No packet header field type: trace must have only | |
653 | * one stream. At this point the stream class being | |
654 | * added is not part of the trace yet, so we validate | |
655 | * that the trace contains no stream classes yet. | |
656 | */ | |
657 | if (trace->stream_classes->len >= 1) { | |
658 | BT_LOGW_STR("Invalid packet header field type: " | |
659 | "packet header field type does not exist but there's more than one stream class in the trace."); | |
660 | goto invalid; | |
661 | } | |
662 | ||
663 | /* No packet header field type: valid at this point */ | |
664 | goto end; | |
665 | } | |
666 | ||
667 | /* Packet header field type, if it exists, must be a structure */ | |
50842bdc | 668 | if (!bt_field_type_is_structure(packet_header_type)) { |
e011d2c1 PP |
669 | BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: " |
670 | "ft-addr=%p, ft-id=%s", | |
671 | packet_header_type, | |
50842bdc | 672 | bt_field_type_id_string(packet_header_type->id)); |
e011d2c1 PP |
673 | goto invalid; |
674 | } | |
675 | ||
676 | /* | |
677 | * If there's a `magic` field, it must be a 32-bit unsigned | |
678 | * integer field type. Also it must be the first field of the | |
679 | * packet header field type. | |
680 | */ | |
50842bdc | 681 | field_type = bt_field_type_structure_get_field_type_by_name( |
e011d2c1 PP |
682 | packet_header_type, "magic"); |
683 | if (field_type) { | |
684 | const char *field_name; | |
685 | ||
50842bdc | 686 | if (!bt_field_type_is_integer(field_type)) { |
e011d2c1 PP |
687 | BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: " |
688 | "magic-ft-addr=%p, magic-ft-id=%s", | |
689 | field_type, | |
50842bdc | 690 | bt_field_type_id_string(field_type->id)); |
e011d2c1 PP |
691 | goto invalid; |
692 | } | |
693 | ||
50842bdc | 694 | if (bt_field_type_integer_is_signed(field_type)) { |
e011d2c1 PP |
695 | BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: " |
696 | "magic-ft-addr=%p", field_type); | |
697 | goto invalid; | |
698 | } | |
699 | ||
50842bdc | 700 | if (bt_field_type_integer_get_size(field_type) != 32) { |
e011d2c1 PP |
701 | BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: " |
702 | "magic-ft-addr=%p, magic-ft-size=%u", | |
703 | field_type, | |
50842bdc | 704 | bt_field_type_integer_get_size(field_type)); |
e011d2c1 PP |
705 | goto invalid; |
706 | } | |
707 | ||
50842bdc | 708 | ret = bt_field_type_structure_get_field_by_index( |
e011d2c1 PP |
709 | packet_header_type, &field_name, NULL, 0); |
710 | assert(ret == 0); | |
711 | ||
712 | if (strcmp(field_name, "magic") != 0) { | |
713 | BT_LOGW("Invalid packet header field type: `magic` field must be the first field: " | |
714 | "magic-ft-addr=%p, first-field-name=\"%s\"", | |
715 | field_type, field_name); | |
716 | goto invalid; | |
717 | } | |
718 | ||
719 | BT_PUT(field_type); | |
720 | } | |
721 | ||
722 | /* | |
723 | * If there's a `uuid` field, it must be an array field type of | |
724 | * length 16 with an 8-bit unsigned integer element field type. | |
725 | */ | |
50842bdc | 726 | field_type = bt_field_type_structure_get_field_type_by_name( |
e011d2c1 PP |
727 | packet_header_type, "uuid"); |
728 | if (field_type) { | |
50842bdc | 729 | struct bt_field_type *elem_ft; |
e011d2c1 | 730 | |
50842bdc | 731 | if (!bt_field_type_is_array(field_type)) { |
e011d2c1 PP |
732 | BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: " |
733 | "uuid-ft-addr=%p, uuid-ft-id=%s", | |
734 | field_type, | |
50842bdc | 735 | bt_field_type_id_string(field_type->id)); |
e011d2c1 PP |
736 | goto invalid; |
737 | } | |
738 | ||
50842bdc | 739 | if (bt_field_type_array_get_length(field_type) != 16) { |
e011d2c1 PP |
740 | BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: " |
741 | "uuid-ft-addr=%p, uuid-ft-length=%" PRId64, | |
742 | field_type, | |
50842bdc | 743 | bt_field_type_array_get_length(field_type)); |
e011d2c1 PP |
744 | goto invalid; |
745 | } | |
746 | ||
50842bdc | 747 | elem_ft = bt_field_type_array_get_element_type(field_type); |
e011d2c1 PP |
748 | assert(elem_ft); |
749 | ||
50842bdc | 750 | if (!bt_field_type_is_integer(elem_ft)) { |
e011d2c1 PP |
751 | BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: " |
752 | "elem-ft-addr=%p, elem-ft-id=%s", | |
753 | elem_ft, | |
50842bdc | 754 | bt_field_type_id_string(elem_ft->id)); |
e011d2c1 PP |
755 | bt_put(elem_ft); |
756 | goto invalid; | |
757 | } | |
758 | ||
50842bdc | 759 | if (bt_field_type_integer_is_signed(elem_ft)) { |
e011d2c1 PP |
760 | BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: " |
761 | "elem-ft-addr=%p", elem_ft); | |
762 | bt_put(elem_ft); | |
763 | goto invalid; | |
764 | } | |
765 | ||
50842bdc | 766 | if (bt_field_type_integer_get_size(elem_ft) != 8) { |
e011d2c1 PP |
767 | BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: " |
768 | "elem-ft-addr=%p, elem-ft-size=%u", | |
769 | elem_ft, | |
50842bdc | 770 | bt_field_type_integer_get_size(elem_ft)); |
e011d2c1 PP |
771 | bt_put(elem_ft); |
772 | goto invalid; | |
773 | } | |
774 | ||
775 | bt_put(elem_ft); | |
776 | BT_PUT(field_type); | |
777 | } | |
778 | ||
779 | /* | |
780 | * The `stream_id` field must exist if there's more than one | |
781 | * stream classes in the trace. | |
782 | */ | |
50842bdc | 783 | field_type = bt_field_type_structure_get_field_type_by_name( |
e011d2c1 PP |
784 | packet_header_type, "stream_id"); |
785 | ||
786 | if (!field_type && trace->stream_classes->len >= 1) { | |
787 | BT_LOGW_STR("Invalid packet header field type: " | |
788 | "`stream_id` field does not exist but there's more than one stream class in the trace."); | |
789 | goto invalid; | |
790 | } | |
791 | ||
792 | /* | |
793 | * If there's a `stream_id` field, it must be an unsigned | |
794 | * integer field type. | |
795 | */ | |
796 | if (field_type) { | |
50842bdc | 797 | if (!bt_field_type_is_integer(field_type)) { |
e011d2c1 PP |
798 | BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: " |
799 | "stream-id-ft-addr=%p, stream-id-ft-id=%s", | |
800 | field_type, | |
50842bdc | 801 | bt_field_type_id_string(field_type->id)); |
e011d2c1 PP |
802 | goto invalid; |
803 | } | |
804 | ||
50842bdc | 805 | if (bt_field_type_integer_is_signed(field_type)) { |
e011d2c1 PP |
806 | BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: " |
807 | "stream-id-ft-addr=%p", field_type); | |
808 | goto invalid; | |
809 | } | |
810 | ||
811 | BT_PUT(field_type); | |
812 | } | |
813 | ||
708ab211 PP |
814 | /* |
815 | * If there's a `packet_seq_num` field, it must be an unsigned | |
816 | * integer field type. | |
817 | */ | |
50842bdc | 818 | field_type = bt_field_type_structure_get_field_type_by_name( |
708ab211 PP |
819 | packet_header_type, "packet_seq_num"); |
820 | if (field_type) { | |
50842bdc | 821 | if (!bt_field_type_is_integer(field_type)) { |
708ab211 PP |
822 | BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: " |
823 | "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s", | |
824 | field_type, | |
50842bdc | 825 | bt_field_type_id_string(field_type->id)); |
708ab211 PP |
826 | goto invalid; |
827 | } | |
828 | ||
50842bdc | 829 | if (bt_field_type_integer_is_signed(field_type)) { |
708ab211 PP |
830 | BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: " |
831 | "packet-seq-num-ft-addr=%p", field_type); | |
832 | goto invalid; | |
833 | } | |
834 | ||
835 | BT_PUT(field_type); | |
836 | } | |
837 | ||
e011d2c1 PP |
838 | goto end; |
839 | ||
840 | invalid: | |
841 | is_valid = false; | |
842 | ||
843 | end: | |
844 | bt_put(field_type); | |
845 | return is_valid; | |
846 | } | |
847 | ||
848 | static | |
50842bdc PP |
849 | bool packet_context_field_type_is_valid(struct bt_trace *trace, |
850 | struct bt_stream_class *stream_class, | |
851 | struct bt_field_type *packet_context_type) | |
e011d2c1 PP |
852 | { |
853 | bool is_valid = true; | |
50842bdc | 854 | struct bt_field_type *field_type = NULL; |
e011d2c1 PP |
855 | |
856 | if (!packet_context_type) { | |
857 | /* No packet context field type: valid at this point */ | |
858 | goto end; | |
859 | } | |
860 | ||
861 | /* Packet context field type, if it exists, must be a structure */ | |
50842bdc | 862 | if (!bt_field_type_is_structure(packet_context_type)) { |
e011d2c1 PP |
863 | BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: " |
864 | "ft-addr=%p, ft-id=%s", | |
865 | packet_context_type, | |
50842bdc | 866 | bt_field_type_id_string(packet_context_type->id)); |
e011d2c1 PP |
867 | goto invalid; |
868 | } | |
869 | ||
870 | /* | |
871 | * If there's a `packet_size` field, it must be an unsigned | |
872 | * integer field type. | |
873 | */ | |
50842bdc | 874 | field_type = bt_field_type_structure_get_field_type_by_name( |
e011d2c1 PP |
875 | packet_context_type, "packet_size"); |
876 | if (field_type) { | |
50842bdc | 877 | if (!bt_field_type_is_integer(field_type)) { |
e011d2c1 PP |
878 | BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: " |
879 | "packet-size-ft-addr=%p, packet-size-ft-id=%s", | |
880 | field_type, | |
50842bdc | 881 | bt_field_type_id_string(field_type->id)); |
e011d2c1 PP |
882 | goto invalid; |
883 | } | |
884 | ||
50842bdc | 885 | if (bt_field_type_integer_is_signed(field_type)) { |
e011d2c1 PP |
886 | BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: " |
887 | "packet-size-ft-addr=%p", field_type); | |
888 | goto invalid; | |
889 | } | |
890 | ||
891 | BT_PUT(field_type); | |
892 | } | |
893 | ||
894 | /* | |
895 | * If there's a `content_size` field, it must be an unsigned | |
896 | * integer field type. | |
897 | */ | |
50842bdc | 898 | field_type = bt_field_type_structure_get_field_type_by_name( |
e011d2c1 PP |
899 | packet_context_type, "content_size"); |
900 | if (field_type) { | |
50842bdc | 901 | if (!bt_field_type_is_integer(field_type)) { |
e011d2c1 PP |
902 | BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: " |
903 | "content-size-ft-addr=%p, content-size-ft-id=%s", | |
904 | field_type, | |
50842bdc | 905 | bt_field_type_id_string(field_type->id)); |
e011d2c1 PP |
906 | goto invalid; |
907 | } | |
908 | ||
50842bdc | 909 | if (bt_field_type_integer_is_signed(field_type)) { |
e011d2c1 PP |
910 | BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: " |
911 | "content-size-ft-addr=%p", field_type); | |
912 | goto invalid; | |
913 | } | |
914 | ||
915 | BT_PUT(field_type); | |
916 | } | |
917 | ||
918 | /* | |
919 | * If there's a `events_discarded` field, it must be an unsigned | |
920 | * integer field type. | |
921 | */ | |
50842bdc | 922 | field_type = bt_field_type_structure_get_field_type_by_name( |
e011d2c1 PP |
923 | packet_context_type, "events_discarded"); |
924 | if (field_type) { | |
50842bdc | 925 | if (!bt_field_type_is_integer(field_type)) { |
e011d2c1 PP |
926 | BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: " |
927 | "events-discarded-ft-addr=%p, events-discarded-ft-id=%s", | |
928 | field_type, | |
50842bdc | 929 | bt_field_type_id_string(field_type->id)); |
e011d2c1 PP |
930 | goto invalid; |
931 | } | |
932 | ||
50842bdc | 933 | if (bt_field_type_integer_is_signed(field_type)) { |
e011d2c1 PP |
934 | BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: " |
935 | "events-discarded-ft-addr=%p", field_type); | |
936 | goto invalid; | |
937 | } | |
938 | ||
939 | BT_PUT(field_type); | |
940 | } | |
941 | ||
942 | /* | |
943 | * If there's a `timestamp_begin` field, it must be an unsigned | |
944 | * integer field type. Also, if the trace is not a CTF writer's | |
945 | * trace, then we cannot automatically set the mapped clock | |
946 | * class of this field, so it must have a mapped clock class. | |
947 | */ | |
50842bdc | 948 | field_type = bt_field_type_structure_get_field_type_by_name( |
e011d2c1 PP |
949 | packet_context_type, "timestamp_begin"); |
950 | if (field_type) { | |
50842bdc | 951 | if (!bt_field_type_is_integer(field_type)) { |
e011d2c1 PP |
952 | BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: " |
953 | "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s", | |
954 | field_type, | |
50842bdc | 955 | bt_field_type_id_string(field_type->id)); |
e011d2c1 PP |
956 | goto invalid; |
957 | } | |
958 | ||
50842bdc | 959 | if (bt_field_type_integer_is_signed(field_type)) { |
e011d2c1 PP |
960 | BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: " |
961 | "timestamp-begin-ft-addr=%p", field_type); | |
962 | goto invalid; | |
963 | } | |
964 | ||
965 | if (!trace->is_created_by_writer) { | |
50842bdc PP |
966 | struct bt_clock_class *clock_class = |
967 | bt_field_type_integer_get_mapped_clock_class( | |
e011d2c1 PP |
968 | field_type); |
969 | ||
970 | bt_put(clock_class); | |
971 | if (!clock_class) { | |
972 | BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: " | |
973 | "timestamp-begin-ft-addr=%p", field_type); | |
974 | goto invalid; | |
975 | } | |
976 | } | |
977 | ||
978 | BT_PUT(field_type); | |
979 | } | |
980 | ||
981 | /* | |
982 | * If there's a `timestamp_end` field, it must be an unsigned | |
983 | * integer field type. Also, if the trace is not a CTF writer's | |
984 | * trace, then we cannot automatically set the mapped clock | |
985 | * class of this field, so it must have a mapped clock class. | |
986 | */ | |
50842bdc | 987 | field_type = bt_field_type_structure_get_field_type_by_name( |
e011d2c1 PP |
988 | packet_context_type, "timestamp_end"); |
989 | if (field_type) { | |
50842bdc | 990 | if (!bt_field_type_is_integer(field_type)) { |
e011d2c1 PP |
991 | BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: " |
992 | "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s", | |
993 | field_type, | |
50842bdc | 994 | bt_field_type_id_string(field_type->id)); |
e011d2c1 PP |
995 | goto invalid; |
996 | } | |
997 | ||
50842bdc | 998 | if (bt_field_type_integer_is_signed(field_type)) { |
e011d2c1 PP |
999 | BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: " |
1000 | "timestamp-end-ft-addr=%p", field_type); | |
1001 | goto invalid; | |
1002 | } | |
1003 | ||
1004 | if (!trace->is_created_by_writer) { | |
50842bdc PP |
1005 | struct bt_clock_class *clock_class = |
1006 | bt_field_type_integer_get_mapped_clock_class( | |
e011d2c1 PP |
1007 | field_type); |
1008 | ||
1009 | bt_put(clock_class); | |
1010 | if (!clock_class) { | |
1011 | BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: " | |
1012 | "timestamp-end-ft-addr=%p", field_type); | |
1013 | goto invalid; | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | BT_PUT(field_type); | |
1018 | } | |
1019 | ||
1020 | goto end; | |
1021 | ||
1022 | invalid: | |
1023 | is_valid = false; | |
1024 | ||
1025 | end: | |
1026 | bt_put(field_type); | |
1027 | return is_valid; | |
1028 | } | |
1029 | ||
1030 | static | |
50842bdc PP |
1031 | bool event_header_field_type_is_valid(struct bt_trace *trace, |
1032 | struct bt_stream_class *stream_class, | |
1033 | struct bt_field_type *event_header_type) | |
e011d2c1 PP |
1034 | { |
1035 | bool is_valid = true; | |
50842bdc | 1036 | struct bt_field_type *field_type = NULL; |
e011d2c1 PP |
1037 | |
1038 | /* | |
1039 | * We do not validate that the `timestamp` field exists here | |
1040 | * because CTF does not require this exact name to be mapped to | |
1041 | * a clock class. | |
1042 | */ | |
1043 | ||
1044 | if (!event_header_type) { | |
1045 | /* | |
1046 | * No event header field type: stream class must have | |
1047 | * only one event class. | |
1048 | */ | |
50842bdc | 1049 | if (bt_stream_class_get_event_class_count(stream_class) > 1) { |
e011d2c1 PP |
1050 | BT_LOGW_STR("Invalid event header field type: " |
1051 | "event header field type does not exist but there's more than one event class in the stream class."); | |
1052 | goto invalid; | |
1053 | } | |
1054 | ||
1055 | /* No event header field type: valid at this point */ | |
1056 | goto end; | |
1057 | } | |
1058 | ||
1059 | /* Event header field type, if it exists, must be a structure */ | |
50842bdc | 1060 | if (!bt_field_type_is_structure(event_header_type)) { |
e011d2c1 PP |
1061 | BT_LOGW("Invalid event header field type: must be a structure field type if it exists: " |
1062 | "ft-addr=%p, ft-id=%s", | |
1063 | event_header_type, | |
50842bdc | 1064 | bt_field_type_id_string(event_header_type->id)); |
e011d2c1 PP |
1065 | goto invalid; |
1066 | } | |
1067 | ||
1068 | /* | |
1069 | * If there's an `id` field, it must be an unsigned integer | |
1070 | * field type or an enumeration field type with an unsigned | |
1071 | * integer container field type. | |
1072 | */ | |
50842bdc | 1073 | field_type = bt_field_type_structure_get_field_type_by_name( |
e011d2c1 PP |
1074 | event_header_type, "id"); |
1075 | if (field_type) { | |
50842bdc | 1076 | struct bt_field_type *int_ft; |
e011d2c1 | 1077 | |
50842bdc | 1078 | if (bt_field_type_is_integer(field_type)) { |
e011d2c1 | 1079 | int_ft = bt_get(field_type); |
50842bdc PP |
1080 | } else if (bt_field_type_is_enumeration(field_type)) { |
1081 | int_ft = bt_field_type_enumeration_get_container_type( | |
e011d2c1 PP |
1082 | field_type); |
1083 | } else { | |
1084 | BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: " | |
1085 | "id-ft-addr=%p, id-ft-id=%s", | |
1086 | field_type, | |
50842bdc | 1087 | bt_field_type_id_string(field_type->id)); |
e011d2c1 PP |
1088 | goto invalid; |
1089 | } | |
1090 | ||
1091 | assert(int_ft); | |
50842bdc | 1092 | if (bt_field_type_integer_is_signed(int_ft)) { |
e011d2c1 PP |
1093 | BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: " |
1094 | "id-ft-addr=%p", int_ft); | |
1095 | goto invalid; | |
1096 | } | |
1097 | ||
1098 | bt_put(int_ft); | |
1099 | BT_PUT(field_type); | |
1100 | } | |
1101 | ||
1102 | goto end; | |
1103 | ||
1104 | invalid: | |
1105 | is_valid = false; | |
1106 | ||
1107 | end: | |
1108 | bt_put(field_type); | |
1109 | return is_valid; | |
1110 | } | |
1111 | ||
726106d3 PP |
1112 | static |
1113 | int check_packet_header_type_has_no_clock_class(struct bt_trace *trace) | |
1114 | { | |
1115 | int ret = 0; | |
1116 | ||
1117 | if (trace->packet_header_type) { | |
1118 | struct bt_clock_class *clock_class = NULL; | |
1119 | ||
1120 | ret = bt_validate_single_clock_class(trace->packet_header_type, | |
1121 | &clock_class); | |
1122 | bt_put(clock_class); | |
1123 | if (ret || clock_class) { | |
1124 | BT_LOGW("Trace's packet header field type cannot " | |
1125 | "contain a field type which is mapped to " | |
1126 | "a clock class: " | |
1127 | "trace-addr=%p, trace-name=\"%s\", " | |
1128 | "clock-class-name=\"%s\"", | |
1129 | trace, bt_trace_get_name(trace), | |
1130 | clock_class ? | |
1131 | bt_clock_class_get_name(clock_class) : | |
1132 | NULL); | |
1133 | ret = -1; | |
1134 | } | |
1135 | } | |
1136 | ||
1137 | return ret; | |
1138 | } | |
1139 | ||
50842bdc PP |
1140 | int bt_trace_add_stream_class(struct bt_trace *trace, |
1141 | struct bt_stream_class *stream_class) | |
ef0c4a15 | 1142 | { |
544d0515 PP |
1143 | int ret; |
1144 | int64_t i; | |
ef0c4a15 | 1145 | int64_t stream_id; |
50842bdc PP |
1146 | struct bt_validation_output trace_sc_validation_output = { 0 }; |
1147 | struct bt_validation_output *ec_validation_outputs = NULL; | |
1148 | const enum bt_validation_flag trace_sc_validation_flags = | |
1149 | BT_VALIDATION_FLAG_TRACE | | |
1150 | BT_VALIDATION_FLAG_STREAM; | |
1151 | const enum bt_validation_flag ec_validation_flags = | |
1152 | BT_VALIDATION_FLAG_EVENT; | |
1153 | struct bt_field_type *packet_header_type = NULL; | |
1154 | struct bt_field_type *packet_context_type = NULL; | |
1155 | struct bt_field_type *event_header_type = NULL; | |
1156 | struct bt_field_type *stream_event_ctx_type = NULL; | |
544d0515 | 1157 | int64_t event_class_count; |
50842bdc | 1158 | struct bt_trace *current_parent_trace = NULL; |
2a3ced3c | 1159 | struct bt_clock_class *expected_clock_class = NULL; |
ef0c4a15 | 1160 | |
95c09b3a PP |
1161 | if (!trace) { |
1162 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
1163 | ret = -1; | |
1164 | goto end; | |
1165 | } | |
1166 | ||
1167 | if (!stream_class) { | |
1168 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
1169 | ret = -1; | |
1170 | goto end; | |
1171 | } | |
1172 | ||
1173 | if (trace->is_static) { | |
1174 | BT_LOGW_STR("Invalid parameter: trace is static."); | |
ef0c4a15 JG |
1175 | ret = -1; |
1176 | goto end; | |
1177 | } | |
1178 | ||
95c09b3a PP |
1179 | BT_LOGD("Adding stream class to trace: " |
1180 | "trace-addr=%p, trace-name=\"%s\", " | |
1181 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
1182 | "stream-class-id=%" PRId64, | |
50842bdc PP |
1183 | trace, bt_trace_get_name(trace), |
1184 | stream_class, bt_stream_class_get_name(stream_class), | |
1185 | bt_stream_class_get_id(stream_class)); | |
95c09b3a | 1186 | |
50842bdc | 1187 | current_parent_trace = bt_stream_class_get_trace(stream_class); |
2789e5d9 JG |
1188 | if (current_parent_trace) { |
1189 | /* Stream class is already associated to a trace, abort. */ | |
95c09b3a PP |
1190 | BT_LOGW("Invalid parameter: stream class is already part of a trace: " |
1191 | "stream-class-trace-addr=%p, " | |
1192 | "stream-class-trace-name=\"%s\"", | |
1193 | current_parent_trace, | |
50842bdc | 1194 | bt_trace_get_name(current_parent_trace)); |
2789e5d9 JG |
1195 | ret = -1; |
1196 | goto end; | |
1197 | } | |
1198 | ||
09840de5 | 1199 | event_class_count = |
50842bdc | 1200 | bt_stream_class_get_event_class_count(stream_class); |
09840de5 PP |
1201 | assert(event_class_count >= 0); |
1202 | ||
fe13b5c0 | 1203 | if (stream_class->clock) { |
50842bdc | 1204 | struct bt_clock_class *stream_clock_class = |
ac0c6bdd PP |
1205 | stream_class->clock->clock_class; |
1206 | ||
1207 | if (trace->is_created_by_writer) { | |
1208 | /* | |
1209 | * Make sure this clock was also added to the | |
1210 | * trace (potentially through its CTF writer | |
1211 | * owner). | |
1212 | */ | |
1213 | size_t i; | |
1214 | ||
1215 | for (i = 0; i < trace->clocks->len; i++) { | |
1216 | if (trace->clocks->pdata[i] == | |
1217 | stream_clock_class) { | |
1218 | /* Found! */ | |
1219 | break; | |
1220 | } | |
1221 | } | |
1222 | ||
1223 | if (i == trace->clocks->len) { | |
1224 | /* Not found */ | |
95c09b3a PP |
1225 | BT_LOGW("Stream class's clock's class is not part of the trace: " |
1226 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
1227 | stream_clock_class, | |
50842bdc | 1228 | bt_clock_class_get_name(stream_clock_class)); |
fe13b5c0 PP |
1229 | ret = -1; |
1230 | goto end; | |
1231 | } | |
1232 | } else { | |
ac0c6bdd PP |
1233 | /* |
1234 | * This trace was NOT created by a CTF writer, | |
1235 | * thus do not allow the stream class to add to | |
1236 | * have a clock at all. Those are two | |
1237 | * independent APIs (non-writer and writer | |
1238 | * APIs), and isolating them simplifies things. | |
1239 | */ | |
95c09b3a PP |
1240 | BT_LOGW("Cannot add stream class with a clock to a trace which was not created by a CTF writer object: " |
1241 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
1242 | stream_clock_class, | |
50842bdc | 1243 | bt_clock_class_get_name(stream_clock_class)); |
ac0c6bdd PP |
1244 | ret = -1; |
1245 | goto end; | |
fe13b5c0 | 1246 | } |
2a3ced3c PP |
1247 | |
1248 | if (stream_class->clock_class && | |
1249 | stream_class->clock_class != | |
1250 | stream_class->clock->clock_class) { | |
1251 | /* | |
1252 | * Stream class already has an expected clock | |
1253 | * class, but it does not match its clock's | |
1254 | * class. | |
1255 | */ | |
1256 | BT_LOGW("Invalid parameter: stream class's clock's " | |
1257 | "class does not match stream class's " | |
1258 | "expected clock class: " | |
1259 | "stream-class-addr=%p, " | |
1260 | "stream-class-id=%" PRId64 ", " | |
1261 | "stream-class-name=\"%s\", " | |
1262 | "expected-clock-class-addr=%p, " | |
1263 | "expected-clock-class-name=\"%s\"", | |
1264 | stream_class, | |
1265 | bt_stream_class_get_id(stream_class), | |
1266 | bt_stream_class_get_name(stream_class), | |
1267 | expected_clock_class, | |
1268 | bt_clock_class_get_name(expected_clock_class)); | |
1269 | } else if (!stream_class->clock_class) { | |
1270 | /* | |
1271 | * Set expected clock class to stream class's | |
1272 | * clock's class. | |
1273 | */ | |
1274 | expected_clock_class = | |
1275 | bt_get(stream_class->clock->clock_class); | |
1276 | } | |
1277 | } | |
1278 | ||
1279 | if (!stream_class->frozen) { | |
1280 | /* | |
1281 | * Stream class is not frozen yet. Validate that the | |
1282 | * stream class contains at most a single clock class | |
1283 | * because the previous | |
1284 | * bt_stream_class_add_event_class() calls did not make | |
1285 | * this validation since the stream class's direct field | |
1286 | * types (packet context, event header, event context) | |
1287 | * could change afterwards. This stream class is about | |
1288 | * to be frozen and those field types won't be changed | |
1289 | * if this function succeeds. | |
1290 | * | |
1291 | * At this point we're also sure that the stream class's | |
1292 | * clock, if any, has the same class as the stream | |
1293 | * class's expected clock class, if any. This is why, if | |
1294 | * bt_stream_class_validate_single_clock_class() | |
1295 | * succeeds below, the call to | |
1296 | * bt_stream_class_map_clock_class() at the end of this | |
1297 | * function is safe because it maps to the same, single | |
1298 | * clock class. | |
1299 | */ | |
1300 | ret = bt_stream_class_validate_single_clock_class(stream_class, | |
1301 | &expected_clock_class); | |
1302 | if (ret) { | |
1303 | BT_LOGW("Invalid parameter: stream class or one of its " | |
1304 | "event classes contains a field type which is " | |
1305 | "not recursively mapped to the expected " | |
1306 | "clock class: " | |
1307 | "stream-class-addr=%p, " | |
1308 | "stream-class-id=%" PRId64 ", " | |
1309 | "stream-class-name=\"%s\", " | |
1310 | "expected-clock-class-addr=%p, " | |
1311 | "expected-clock-class-name=\"%s\"", | |
1312 | stream_class, bt_stream_class_get_id(stream_class), | |
1313 | bt_stream_class_get_name(stream_class), | |
1314 | expected_clock_class, | |
1315 | expected_clock_class ? | |
1316 | bt_clock_class_get_name(expected_clock_class) : | |
1317 | NULL); | |
1318 | goto end; | |
1319 | } | |
fe13b5c0 PP |
1320 | } |
1321 | ||
726106d3 PP |
1322 | ret = check_packet_header_type_has_no_clock_class(trace); |
1323 | if (ret) { | |
1324 | /* check_packet_header_type_has_no_clock_class() logs errors */ | |
1325 | goto end; | |
1326 | } | |
1327 | ||
09840de5 PP |
1328 | /* |
1329 | * We're about to freeze both the trace and the stream class. | |
1330 | * Also, each event class contained in this stream class are | |
1331 | * already frozen. | |
1332 | * | |
1333 | * This trace, this stream class, and all its event classes | |
1334 | * should be valid at this point. | |
1335 | * | |
1336 | * Validate trace and stream class first, then each event | |
1337 | * class of this stream class can be validated individually. | |
1338 | */ | |
1339 | packet_header_type = | |
50842bdc | 1340 | bt_trace_get_packet_header_type(trace); |
09840de5 | 1341 | packet_context_type = |
50842bdc | 1342 | bt_stream_class_get_packet_context_type(stream_class); |
09840de5 | 1343 | event_header_type = |
50842bdc | 1344 | bt_stream_class_get_event_header_type(stream_class); |
09840de5 | 1345 | stream_event_ctx_type = |
50842bdc | 1346 | bt_stream_class_get_event_context_type(stream_class); |
95c09b3a PP |
1347 | |
1348 | BT_LOGD("Validating trace and stream class field types."); | |
50842bdc | 1349 | ret = bt_validate_class_types(trace->environment, |
09840de5 PP |
1350 | packet_header_type, packet_context_type, event_header_type, |
1351 | stream_event_ctx_type, NULL, NULL, trace->valid, | |
1352 | stream_class->valid, 1, &trace_sc_validation_output, | |
1353 | trace_sc_validation_flags); | |
1354 | BT_PUT(packet_header_type); | |
1355 | BT_PUT(packet_context_type); | |
1356 | BT_PUT(event_header_type); | |
1357 | BT_PUT(stream_event_ctx_type); | |
1358 | ||
2bd7f864 | 1359 | if (ret) { |
09840de5 PP |
1360 | /* |
1361 | * This means something went wrong during the validation | |
1362 | * process, not that the objects are invalid. | |
1363 | */ | |
95c09b3a PP |
1364 | BT_LOGE("Failed to validate trace and stream class field types: " |
1365 | "ret=%d", ret); | |
2bd7f864 JG |
1366 | goto end; |
1367 | } | |
1368 | ||
09840de5 PP |
1369 | if ((trace_sc_validation_output.valid_flags & |
1370 | trace_sc_validation_flags) != | |
1371 | trace_sc_validation_flags) { | |
1372 | /* Invalid trace/stream class */ | |
95c09b3a PP |
1373 | BT_LOGW("Invalid trace or stream class field types: " |
1374 | "valid-flags=0x%x", | |
1375 | trace_sc_validation_output.valid_flags); | |
09840de5 PP |
1376 | ret = -1; |
1377 | goto end; | |
1378 | } | |
1379 | ||
1380 | if (event_class_count > 0) { | |
50842bdc | 1381 | ec_validation_outputs = g_new0(struct bt_validation_output, |
09840de5 PP |
1382 | event_class_count); |
1383 | if (!ec_validation_outputs) { | |
95c09b3a | 1384 | BT_LOGE_STR("Failed to allocate one validation output structure."); |
09840de5 PP |
1385 | ret = -1; |
1386 | goto end; | |
1387 | } | |
1388 | } | |
1389 | ||
1390 | /* Validate each event class individually */ | |
6474e016 | 1391 | for (i = 0; i < event_class_count; i++) { |
50842bdc PP |
1392 | struct bt_event_class *event_class = |
1393 | bt_stream_class_get_event_class_by_index( | |
9ac68eb1 | 1394 | stream_class, i); |
50842bdc PP |
1395 | struct bt_field_type *event_context_type = NULL; |
1396 | struct bt_field_type *event_payload_type = NULL; | |
09840de5 PP |
1397 | |
1398 | event_context_type = | |
50842bdc | 1399 | bt_event_class_get_context_type(event_class); |
09840de5 | 1400 | event_payload_type = |
50842bdc | 1401 | bt_event_class_get_payload_type(event_class); |
09840de5 PP |
1402 | |
1403 | /* | |
1404 | * It is important to use the field types returned by | |
1405 | * the previous trace and stream class validation here | |
1406 | * because copies could have been made. | |
1407 | */ | |
95c09b3a PP |
1408 | BT_LOGD("Validating event class's field types: " |
1409 | "addr=%p, name=\"%s\", id=%" PRId64, | |
50842bdc PP |
1410 | event_class, bt_event_class_get_name(event_class), |
1411 | bt_event_class_get_id(event_class)); | |
1412 | ret = bt_validate_class_types(trace->environment, | |
09840de5 PP |
1413 | trace_sc_validation_output.packet_header_type, |
1414 | trace_sc_validation_output.packet_context_type, | |
1415 | trace_sc_validation_output.event_header_type, | |
1416 | trace_sc_validation_output.stream_event_ctx_type, | |
1417 | event_context_type, event_payload_type, | |
1418 | 1, 1, event_class->valid, &ec_validation_outputs[i], | |
1419 | ec_validation_flags); | |
1420 | BT_PUT(event_context_type); | |
1421 | BT_PUT(event_payload_type); | |
1422 | BT_PUT(event_class); | |
1423 | ||
1424 | if (ret) { | |
95c09b3a PP |
1425 | BT_LOGE("Failed to validate event class field types: " |
1426 | "ret=%d", ret); | |
09840de5 PP |
1427 | goto end; |
1428 | } | |
1429 | ||
1430 | if ((ec_validation_outputs[i].valid_flags & | |
1431 | ec_validation_flags) != ec_validation_flags) { | |
1432 | /* Invalid event class */ | |
95c09b3a PP |
1433 | BT_LOGW("Invalid event class field types: " |
1434 | "valid-flags=0x%x", | |
1435 | ec_validation_outputs[i].valid_flags); | |
09840de5 PP |
1436 | ret = -1; |
1437 | goto end; | |
1438 | } | |
1439 | } | |
1440 | ||
50842bdc | 1441 | stream_id = bt_stream_class_get_id(stream_class); |
ef0c4a15 JG |
1442 | if (stream_id < 0) { |
1443 | stream_id = trace->next_stream_id++; | |
9ac68eb1 | 1444 | if (stream_id < 0) { |
95c09b3a | 1445 | BT_LOGE_STR("No more stream class IDs available."); |
9ac68eb1 PP |
1446 | ret = -1; |
1447 | goto end; | |
1448 | } | |
ef0c4a15 JG |
1449 | |
1450 | /* Try to assign a new stream id */ | |
1451 | for (i = 0; i < trace->stream_classes->len; i++) { | |
50842bdc | 1452 | if (stream_id == bt_stream_class_get_id( |
ef0c4a15 JG |
1453 | trace->stream_classes->pdata[i])) { |
1454 | /* Duplicate stream id found */ | |
95c09b3a PP |
1455 | BT_LOGW("Duplicate stream class ID: " |
1456 | "id=%" PRId64, (int64_t) stream_id); | |
ef0c4a15 JG |
1457 | ret = -1; |
1458 | goto end; | |
1459 | } | |
1460 | } | |
1461 | ||
50842bdc | 1462 | if (bt_stream_class_set_id_no_check(stream_class, |
95c09b3a | 1463 | stream_id)) { |
ef0c4a15 | 1464 | /* TODO Should retry with a different stream id */ |
95c09b3a PP |
1465 | BT_LOGE("Cannot set stream class's ID: " |
1466 | "id=%" PRId64, (int64_t) stream_id); | |
ef0c4a15 JG |
1467 | ret = -1; |
1468 | goto end; | |
1469 | } | |
1470 | } | |
1471 | ||
e011d2c1 PP |
1472 | /* |
1473 | * At this point all the field types in the validation output | |
1474 | * are valid. Validate the semantics of some scopes according to | |
1475 | * the CTF specification. | |
1476 | */ | |
1477 | if (!packet_header_field_type_is_valid(trace, | |
1478 | trace_sc_validation_output.packet_header_type)) { | |
95c09b3a | 1479 | BT_LOGW_STR("Invalid trace's packet header field type."); |
e011d2c1 PP |
1480 | ret = -1; |
1481 | goto end; | |
1482 | } | |
1483 | ||
1484 | if (!packet_context_field_type_is_valid(trace, | |
1485 | stream_class, | |
1486 | trace_sc_validation_output.packet_context_type)) { | |
95c09b3a | 1487 | BT_LOGW_STR("Invalid stream class's packet context field type."); |
e011d2c1 PP |
1488 | ret = -1; |
1489 | goto end; | |
1490 | } | |
1491 | ||
1492 | if (!event_header_field_type_is_valid(trace, | |
1493 | stream_class, | |
1494 | trace_sc_validation_output.event_header_type)) { | |
95c09b3a | 1495 | BT_LOGW_STR("Invalid steam class's event header field type."); |
e011d2c1 PP |
1496 | ret = -1; |
1497 | goto end; | |
1498 | } | |
1499 | ||
1500 | /* | |
1501 | * Now is the time to automatically map specific field types of | |
1502 | * the stream class's packet context and event header field | |
1503 | * types to the stream class's clock's class if they are not | |
1504 | * mapped to a clock class yet. We do it here because we know | |
1505 | * that after this point, everything is frozen so it won't be | |
1506 | * possible for the user to modify the stream class's clock, or | |
1507 | * to map those field types to other clock classes. | |
1508 | */ | |
1509 | if (trace->is_created_by_writer) { | |
50842bdc | 1510 | if (bt_stream_class_map_clock_class(stream_class, |
e011d2c1 PP |
1511 | trace_sc_validation_output.packet_context_type, |
1512 | trace_sc_validation_output.event_header_type)) { | |
95c09b3a | 1513 | BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class."); |
e011d2c1 PP |
1514 | ret = -1; |
1515 | goto end; | |
1516 | } | |
1517 | } | |
1518 | ||
2a3ced3c PP |
1519 | |
1520 | ||
e6a8e8e4 | 1521 | bt_object_set_parent(stream_class, trace); |
ef0c4a15 JG |
1522 | g_ptr_array_add(trace->stream_classes, stream_class); |
1523 | ||
1524 | /* | |
09840de5 PP |
1525 | * At this point we know that the function will be successful. |
1526 | * Therefore we can replace the trace and stream class field | |
1527 | * types with what's in their validation output structure and | |
1528 | * mark them as valid. We can also replace the field types of | |
1529 | * all the event classes of the stream class and mark them as | |
1530 | * valid. | |
1531 | */ | |
50842bdc | 1532 | bt_validation_replace_types(trace, stream_class, NULL, |
09840de5 PP |
1533 | &trace_sc_validation_output, trace_sc_validation_flags); |
1534 | trace->valid = 1; | |
1535 | stream_class->valid = 1; | |
1536 | ||
1537 | /* | |
50842bdc | 1538 | * Put what was not moved in bt_validation_replace_types(). |
09840de5 | 1539 | */ |
50842bdc | 1540 | bt_validation_output_put_types(&trace_sc_validation_output); |
09840de5 | 1541 | |
6474e016 | 1542 | for (i = 0; i < event_class_count; i++) { |
50842bdc PP |
1543 | struct bt_event_class *event_class = |
1544 | bt_stream_class_get_event_class_by_index( | |
9ac68eb1 | 1545 | stream_class, i); |
09840de5 | 1546 | |
50842bdc | 1547 | bt_validation_replace_types(NULL, NULL, event_class, |
09840de5 PP |
1548 | &ec_validation_outputs[i], ec_validation_flags); |
1549 | event_class->valid = 1; | |
1550 | BT_PUT(event_class); | |
1551 | ||
1552 | /* | |
1553 | * Put what was not moved in | |
50842bdc | 1554 | * bt_validation_replace_types(). |
09840de5 | 1555 | */ |
50842bdc | 1556 | bt_validation_output_put_types(&ec_validation_outputs[i]); |
09840de5 PP |
1557 | } |
1558 | ||
09840de5 PP |
1559 | /* |
1560 | * Freeze the trace and the stream class. | |
1561 | */ | |
50842bdc PP |
1562 | bt_stream_class_freeze(stream_class); |
1563 | bt_trace_freeze(trace); | |
09840de5 | 1564 | |
2a3ced3c PP |
1565 | /* |
1566 | * It is safe to set the stream class's unique clock class | |
1567 | * now because the stream class is frozen. | |
1568 | */ | |
1569 | if (expected_clock_class) { | |
1570 | BT_MOVE(stream_class->clock_class, expected_clock_class); | |
1571 | } | |
1572 | ||
9b888ff3 | 1573 | /* Notifiy listeners of the trace's schema modification. */ |
50842bdc PP |
1574 | bt_stream_class_visit(stream_class, |
1575 | bt_trace_object_modification, trace); | |
95c09b3a PP |
1576 | BT_LOGD("Added stream class to trace: " |
1577 | "trace-addr=%p, trace-name=\"%s\", " | |
1578 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
1579 | "stream-class-id=%" PRId64, | |
50842bdc PP |
1580 | trace, bt_trace_get_name(trace), |
1581 | stream_class, bt_stream_class_get_name(stream_class), | |
1582 | bt_stream_class_get_id(stream_class)); | |
95c09b3a | 1583 | |
ef0c4a15 | 1584 | end: |
d3814b54 | 1585 | if (ret) { |
e6a8e8e4 | 1586 | bt_object_set_parent(stream_class, NULL); |
09840de5 PP |
1587 | |
1588 | if (ec_validation_outputs) { | |
6474e016 | 1589 | for (i = 0; i < event_class_count; i++) { |
50842bdc | 1590 | bt_validation_output_put_types( |
09840de5 PP |
1591 | &ec_validation_outputs[i]); |
1592 | } | |
1593 | } | |
d3814b54 | 1594 | } |
09840de5 PP |
1595 | |
1596 | g_free(ec_validation_outputs); | |
50842bdc | 1597 | bt_validation_output_put_types(&trace_sc_validation_output); |
2789e5d9 | 1598 | bt_put(current_parent_trace); |
2a3ced3c | 1599 | bt_put(expected_clock_class); |
09840de5 PP |
1600 | assert(!packet_header_type); |
1601 | assert(!packet_context_type); | |
1602 | assert(!event_header_type); | |
1603 | assert(!stream_event_ctx_type); | |
ef0c4a15 JG |
1604 | return ret; |
1605 | } | |
1606 | ||
50842bdc | 1607 | int64_t bt_trace_get_stream_count(struct bt_trace *trace) |
c1e730fe | 1608 | { |
544d0515 | 1609 | int64_t ret; |
c1e730fe PP |
1610 | |
1611 | if (!trace) { | |
95c09b3a | 1612 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
9ac68eb1 | 1613 | ret = (int64_t) -1; |
c1e730fe PP |
1614 | goto end; |
1615 | } | |
1616 | ||
9ac68eb1 | 1617 | ret = (int64_t) trace->streams->len; |
c1e730fe PP |
1618 | |
1619 | end: | |
1620 | return ret; | |
1621 | } | |
1622 | ||
50842bdc PP |
1623 | struct bt_stream *bt_trace_get_stream_by_index( |
1624 | struct bt_trace *trace, | |
9ac68eb1 | 1625 | uint64_t index) |
c1e730fe | 1626 | { |
50842bdc | 1627 | struct bt_stream *stream = NULL; |
c1e730fe | 1628 | |
95c09b3a PP |
1629 | if (!trace) { |
1630 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
1631 | goto end; | |
1632 | } | |
1633 | ||
1634 | if (index >= trace->streams->len) { | |
1635 | BT_LOGW("Invalid parameter: index is out of bounds: " | |
1636 | "addr=%p, name=\"%s\", " | |
1637 | "index=%" PRIu64 ", count=%u", | |
50842bdc | 1638 | trace, bt_trace_get_name(trace), |
95c09b3a | 1639 | index, trace->streams->len); |
c1e730fe PP |
1640 | goto end; |
1641 | } | |
1642 | ||
1643 | stream = bt_get(g_ptr_array_index(trace->streams, index)); | |
1644 | ||
1645 | end: | |
1646 | return stream; | |
1647 | } | |
1648 | ||
50842bdc | 1649 | int64_t bt_trace_get_stream_class_count(struct bt_trace *trace) |
ef0c4a15 | 1650 | { |
544d0515 | 1651 | int64_t ret; |
ef0c4a15 JG |
1652 | |
1653 | if (!trace) { | |
95c09b3a | 1654 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
9ac68eb1 | 1655 | ret = (int64_t) -1; |
ef0c4a15 JG |
1656 | goto end; |
1657 | } | |
1658 | ||
9ac68eb1 | 1659 | ret = (int64_t) trace->stream_classes->len; |
ef0c4a15 JG |
1660 | end: |
1661 | return ret; | |
1662 | } | |
1663 | ||
50842bdc PP |
1664 | struct bt_stream_class *bt_trace_get_stream_class_by_index( |
1665 | struct bt_trace *trace, uint64_t index) | |
ef0c4a15 | 1666 | { |
50842bdc | 1667 | struct bt_stream_class *stream_class = NULL; |
ef0c4a15 | 1668 | |
95c09b3a PP |
1669 | if (!trace) { |
1670 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
1671 | goto end; | |
1672 | } | |
1673 | ||
1674 | if (index >= trace->stream_classes->len) { | |
1675 | BT_LOGW("Invalid parameter: index is out of bounds: " | |
1676 | "addr=%p, name=\"%s\", " | |
1677 | "index=%" PRIu64 ", count=%u", | |
50842bdc | 1678 | trace, bt_trace_get_name(trace), |
95c09b3a | 1679 | index, trace->stream_classes->len); |
ef0c4a15 JG |
1680 | goto end; |
1681 | } | |
1682 | ||
1683 | stream_class = g_ptr_array_index(trace->stream_classes, index); | |
83509119 | 1684 | bt_get(stream_class); |
ef0c4a15 JG |
1685 | end: |
1686 | return stream_class; | |
1687 | } | |
1688 | ||
50842bdc PP |
1689 | struct bt_stream_class *bt_trace_get_stream_class_by_id( |
1690 | struct bt_trace *trace, uint64_t id_param) | |
4841ccc1 PP |
1691 | { |
1692 | int i; | |
50842bdc | 1693 | struct bt_stream_class *stream_class = NULL; |
9ac68eb1 | 1694 | int64_t id = (int64_t) id_param; |
4841ccc1 | 1695 | |
95c09b3a PP |
1696 | if (!trace) { |
1697 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
1698 | goto end; | |
1699 | } | |
1700 | ||
1701 | if (id < 0) { | |
1702 | BT_LOGW("Invalid parameter: invalid stream class's ID: " | |
1703 | "trace-addr=%p, trace-name=\"%s\", id=%" PRIu64, | |
50842bdc | 1704 | trace, bt_trace_get_name(trace), id_param); |
4841ccc1 PP |
1705 | goto end; |
1706 | } | |
1707 | ||
6474e016 | 1708 | for (i = 0; i < trace->stream_classes->len; i++) { |
50842bdc | 1709 | struct bt_stream_class *stream_class_candidate; |
4841ccc1 PP |
1710 | |
1711 | stream_class_candidate = | |
1712 | g_ptr_array_index(trace->stream_classes, i); | |
1713 | ||
50842bdc | 1714 | if (bt_stream_class_get_id(stream_class_candidate) == |
4841ccc1 PP |
1715 | (int64_t) id) { |
1716 | stream_class = stream_class_candidate; | |
83509119 | 1717 | bt_get(stream_class); |
4841ccc1 PP |
1718 | goto end; |
1719 | } | |
1720 | } | |
1721 | ||
1722 | end: | |
1723 | return stream_class; | |
1724 | } | |
1725 | ||
50842bdc PP |
1726 | struct bt_clock_class *bt_trace_get_clock_class_by_name( |
1727 | struct bt_trace *trace, const char *name) | |
7e4347a3 PP |
1728 | { |
1729 | size_t i; | |
50842bdc | 1730 | struct bt_clock_class *clock_class = NULL; |
7e4347a3 | 1731 | |
95c09b3a PP |
1732 | if (!trace) { |
1733 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
1734 | goto end; | |
1735 | } | |
1736 | ||
1737 | if (!name) { | |
1738 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
7e4347a3 PP |
1739 | goto end; |
1740 | } | |
1741 | ||
6474e016 | 1742 | for (i = 0; i < trace->clocks->len; i++) { |
50842bdc | 1743 | struct bt_clock_class *cur_clk = |
7e4347a3 | 1744 | g_ptr_array_index(trace->clocks, i); |
50842bdc | 1745 | const char *cur_clk_name = bt_clock_class_get_name(cur_clk); |
7e4347a3 PP |
1746 | |
1747 | if (!cur_clk_name) { | |
1748 | goto end; | |
1749 | } | |
1750 | ||
1751 | if (!strcmp(cur_clk_name, name)) { | |
ac0c6bdd PP |
1752 | clock_class = cur_clk; |
1753 | bt_get(clock_class); | |
7e4347a3 PP |
1754 | goto end; |
1755 | } | |
1756 | } | |
1757 | ||
1758 | end: | |
ac0c6bdd | 1759 | return clock_class; |
7e4347a3 PP |
1760 | } |
1761 | ||
c9d90a34 | 1762 | BT_HIDDEN |
50842bdc PP |
1763 | bt_bool bt_trace_has_clock_class(struct bt_trace *trace, |
1764 | struct bt_clock_class *clock_class) | |
c9d90a34 PP |
1765 | { |
1766 | struct search_query query = { .value = clock_class, .found = 0 }; | |
1767 | ||
1768 | assert(trace); | |
1769 | assert(clock_class); | |
1770 | ||
1771 | g_ptr_array_foreach(trace->clocks, value_exists, &query); | |
1772 | return query.found; | |
1773 | } | |
1774 | ||
bc37ae52 | 1775 | BT_HIDDEN |
50842bdc | 1776 | const char *get_byte_order_string(enum bt_byte_order byte_order) |
bc37ae52 JG |
1777 | { |
1778 | const char *string; | |
1779 | ||
1780 | switch (byte_order) { | |
50842bdc | 1781 | case BT_BYTE_ORDER_LITTLE_ENDIAN: |
bc37ae52 JG |
1782 | string = "le"; |
1783 | break; | |
50842bdc | 1784 | case BT_BYTE_ORDER_BIG_ENDIAN: |
bc37ae52 JG |
1785 | string = "be"; |
1786 | break; | |
50842bdc | 1787 | case BT_BYTE_ORDER_NATIVE: |
dc3fffef | 1788 | string = "native"; |
bc37ae52 | 1789 | break; |
dc3fffef | 1790 | default: |
0fbb9a9f | 1791 | abort(); |
bc37ae52 JG |
1792 | } |
1793 | ||
1794 | return string; | |
1795 | } | |
1796 | ||
1797 | static | |
50842bdc | 1798 | int append_trace_metadata(struct bt_trace *trace, |
bc37ae52 JG |
1799 | struct metadata_context *context) |
1800 | { | |
1801 | unsigned char *uuid = trace->uuid; | |
e011d2c1 | 1802 | int ret = 0; |
bc37ae52 | 1803 | |
50842bdc PP |
1804 | if (trace->native_byte_order == BT_BYTE_ORDER_NATIVE || |
1805 | trace->native_byte_order == BT_BYTE_ORDER_UNSPECIFIED) { | |
1806 | BT_LOGW("Invalid parameter: trace's byte order cannot be BT_BYTE_ORDER_NATIVE or BT_BYTE_ORDER_UNSPECIFIED at this point; " | |
1807 | "set it with bt_trace_set_native_byte_order(): " | |
95c09b3a | 1808 | "addr=%p, name=\"%s\"", |
50842bdc | 1809 | trace, bt_trace_get_name(trace)); |
4a32fda0 PP |
1810 | ret = -1; |
1811 | goto end; | |
1812 | } | |
bc37ae52 | 1813 | |
4a32fda0 | 1814 | g_string_append(context->string, "trace {\n"); |
bc37ae52 JG |
1815 | g_string_append(context->string, "\tmajor = 1;\n"); |
1816 | g_string_append(context->string, "\tminor = 8;\n"); | |
50842bdc PP |
1817 | assert(trace->native_byte_order == BT_BYTE_ORDER_LITTLE_ENDIAN || |
1818 | trace->native_byte_order == BT_BYTE_ORDER_BIG_ENDIAN || | |
1819 | trace->native_byte_order == BT_BYTE_ORDER_NETWORK); | |
4a32fda0 PP |
1820 | |
1821 | if (trace->uuid_set) { | |
1822 | g_string_append_printf(context->string, | |
1823 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
1824 | uuid[0], uuid[1], uuid[2], uuid[3], | |
1825 | uuid[4], uuid[5], uuid[6], uuid[7], | |
1826 | uuid[8], uuid[9], uuid[10], uuid[11], | |
1827 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
1828 | } | |
1829 | ||
bc37ae52 | 1830 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", |
dc3fffef | 1831 | get_byte_order_string(trace->native_byte_order)); |
bc37ae52 | 1832 | |
e011d2c1 PP |
1833 | if (trace->packet_header_type) { |
1834 | g_string_append(context->string, "\tpacket.header := "); | |
1835 | context->current_indentation_level++; | |
1836 | g_string_assign(context->field_name, ""); | |
95c09b3a | 1837 | BT_LOGD_STR("Serializing trace's packet header field type's metadata."); |
50842bdc | 1838 | ret = bt_field_type_serialize(trace->packet_header_type, |
e011d2c1 PP |
1839 | context); |
1840 | if (ret) { | |
1841 | goto end; | |
1842 | } | |
1843 | context->current_indentation_level--; | |
bc37ae52 | 1844 | } |
bc37ae52 JG |
1845 | |
1846 | g_string_append(context->string, ";\n};\n\n"); | |
1847 | end: | |
1848 | return ret; | |
1849 | } | |
1850 | ||
bc37ae52 | 1851 | static |
50842bdc | 1852 | void append_env_metadata(struct bt_trace *trace, |
bc37ae52 JG |
1853 | struct metadata_context *context) |
1854 | { | |
544d0515 PP |
1855 | int64_t i; |
1856 | int64_t env_size; | |
7f800dc7 | 1857 | |
50842bdc | 1858 | env_size = bt_attributes_get_count(trace->environment); |
7f800dc7 | 1859 | if (env_size <= 0) { |
bc37ae52 JG |
1860 | return; |
1861 | } | |
1862 | ||
1863 | g_string_append(context->string, "env {\n"); | |
7f800dc7 | 1864 | |
6474e016 | 1865 | for (i = 0; i < env_size; i++) { |
dac5c838 | 1866 | struct bt_value *env_field_value_obj = NULL; |
7f800dc7 | 1867 | const char *entry_name; |
7f800dc7 | 1868 | |
50842bdc | 1869 | entry_name = bt_attributes_get_field_name( |
7f800dc7 | 1870 | trace->environment, i); |
50842bdc | 1871 | env_field_value_obj = bt_attributes_get_field_value( |
7f800dc7 PP |
1872 | trace->environment, i); |
1873 | ||
95c09b3a PP |
1874 | assert(entry_name); |
1875 | assert(env_field_value_obj); | |
7f800dc7 | 1876 | |
dac5c838 PP |
1877 | switch (bt_value_get_type(env_field_value_obj)) { |
1878 | case BT_VALUE_TYPE_INTEGER: | |
b8248cc0 PP |
1879 | { |
1880 | int ret; | |
1881 | int64_t int_value; | |
1882 | ||
dac5c838 | 1883 | ret = bt_value_integer_get(env_field_value_obj, |
7f800dc7 | 1884 | &int_value); |
95c09b3a | 1885 | assert(ret == 0); |
7f800dc7 PP |
1886 | g_string_append_printf(context->string, |
1887 | "\t%s = %" PRId64 ";\n", entry_name, | |
1888 | int_value); | |
1889 | break; | |
b8248cc0 | 1890 | } |
dac5c838 | 1891 | case BT_VALUE_TYPE_STRING: |
7f800dc7 PP |
1892 | { |
1893 | int ret; | |
1894 | const char *str_value; | |
1895 | char *escaped_str = NULL; | |
1896 | ||
dac5c838 | 1897 | ret = bt_value_string_get(env_field_value_obj, |
7f800dc7 | 1898 | &str_value); |
95c09b3a | 1899 | assert(ret == 0); |
7f800dc7 | 1900 | escaped_str = g_strescape(str_value, NULL); |
7f800dc7 | 1901 | if (!escaped_str) { |
95c09b3a PP |
1902 | BT_LOGE("Cannot escape string: string=\"%s\"", |
1903 | str_value); | |
7f800dc7 PP |
1904 | goto loop_next; |
1905 | } | |
1906 | ||
1907 | g_string_append_printf(context->string, | |
1908 | "\t%s = \"%s\";\n", entry_name, escaped_str); | |
1909 | free(escaped_str); | |
1910 | break; | |
1911 | } | |
7f800dc7 PP |
1912 | default: |
1913 | goto loop_next; | |
1914 | } | |
1915 | ||
1916 | loop_next: | |
83509119 | 1917 | BT_PUT(env_field_value_obj); |
7f800dc7 PP |
1918 | } |
1919 | ||
bc37ae52 JG |
1920 | g_string_append(context->string, "};\n\n"); |
1921 | } | |
1922 | ||
50842bdc | 1923 | char *bt_trace_get_metadata_string(struct bt_trace *trace) |
bc37ae52 JG |
1924 | { |
1925 | char *metadata = NULL; | |
1926 | struct metadata_context *context = NULL; | |
1927 | int err = 0; | |
1928 | size_t i; | |
1929 | ||
1930 | if (!trace) { | |
95c09b3a | 1931 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
bc37ae52 JG |
1932 | goto end; |
1933 | } | |
1934 | ||
1935 | context = g_new0(struct metadata_context, 1); | |
1936 | if (!context) { | |
95c09b3a | 1937 | BT_LOGE_STR("Failed to allocate one metadata context."); |
bc37ae52 JG |
1938 | goto end; |
1939 | } | |
1940 | ||
1941 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); | |
1942 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); | |
1943 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); | |
1944 | if (append_trace_metadata(trace, context)) { | |
95c09b3a | 1945 | /* append_trace_metadata() logs errors */ |
bc37ae52 JG |
1946 | goto error; |
1947 | } | |
1948 | append_env_metadata(trace, context); | |
1949 | g_ptr_array_foreach(trace->clocks, | |
50842bdc | 1950 | (GFunc)bt_clock_class_serialize, context); |
bc37ae52 JG |
1951 | |
1952 | for (i = 0; i < trace->stream_classes->len; i++) { | |
50842bdc PP |
1953 | /* bt_stream_class_serialize() logs details */ |
1954 | err = bt_stream_class_serialize( | |
bc37ae52 JG |
1955 | trace->stream_classes->pdata[i], context); |
1956 | if (err) { | |
50842bdc | 1957 | /* bt_stream_class_serialize() logs errors */ |
bc37ae52 JG |
1958 | goto error; |
1959 | } | |
1960 | } | |
1961 | ||
1962 | metadata = context->string->str; | |
95c09b3a | 1963 | |
bc37ae52 JG |
1964 | error: |
1965 | g_string_free(context->string, err ? TRUE : FALSE); | |
1966 | g_string_free(context->field_name, TRUE); | |
1967 | g_free(context); | |
95c09b3a | 1968 | |
bc37ae52 JG |
1969 | end: |
1970 | return metadata; | |
1971 | } | |
1972 | ||
50842bdc PP |
1973 | enum bt_byte_order bt_trace_get_native_byte_order( |
1974 | struct bt_trace *trace) | |
4ed90fb3 | 1975 | { |
50842bdc | 1976 | enum bt_byte_order ret = BT_BYTE_ORDER_UNKNOWN; |
4ed90fb3 JG |
1977 | |
1978 | if (!trace) { | |
95c09b3a | 1979 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
4ed90fb3 JG |
1980 | goto end; |
1981 | } | |
1982 | ||
dc3fffef PP |
1983 | ret = trace->native_byte_order; |
1984 | ||
4ed90fb3 JG |
1985 | end: |
1986 | return ret; | |
1987 | } | |
1988 | ||
50842bdc PP |
1989 | int bt_trace_set_native_byte_order(struct bt_trace *trace, |
1990 | enum bt_byte_order byte_order) | |
bc37ae52 JG |
1991 | { |
1992 | int ret = 0; | |
bc37ae52 | 1993 | |
95c09b3a PP |
1994 | if (!trace) { |
1995 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
1996 | ret = -1; | |
1997 | goto end; | |
1998 | } | |
1999 | ||
2000 | if (trace->frozen) { | |
2001 | BT_LOGW("Invalid parameter: trace is frozen: " | |
2002 | "addr=%p, name=\"%s\"", | |
50842bdc | 2003 | trace, bt_trace_get_name(trace)); |
bc37ae52 JG |
2004 | ret = -1; |
2005 | goto end; | |
2006 | } | |
2007 | ||
d41cff38 | 2008 | if (trace->is_created_by_writer && |
50842bdc PP |
2009 | byte_order == BT_BYTE_ORDER_UNSPECIFIED) { |
2010 | BT_LOGW("Invalid parameter: BT_BYTE_ORDER_UNSPECIFIED byte order is not allowed for a CTF writer trace: " | |
d41cff38 | 2011 | "addr=%p, name=\"%s\"", |
50842bdc | 2012 | trace, bt_trace_get_name(trace)); |
d41cff38 PP |
2013 | ret = -1; |
2014 | goto end; | |
2015 | } | |
2016 | ||
50842bdc PP |
2017 | if (byte_order != BT_BYTE_ORDER_LITTLE_ENDIAN && |
2018 | byte_order != BT_BYTE_ORDER_BIG_ENDIAN && | |
2019 | byte_order != BT_BYTE_ORDER_NETWORK) { | |
95c09b3a PP |
2020 | BT_LOGW("Invalid parameter: invalid byte order: " |
2021 | "addr=%p, name=\"%s\", bo=%s", | |
50842bdc PP |
2022 | trace, bt_trace_get_name(trace), |
2023 | bt_byte_order_string(byte_order)); | |
bc37ae52 JG |
2024 | ret = -1; |
2025 | goto end; | |
2026 | } | |
2027 | ||
dc3fffef | 2028 | trace->native_byte_order = byte_order; |
95c09b3a PP |
2029 | BT_LOGV("Set trace's native byte order: " |
2030 | "addr=%p, name=\"%s\", bo=%s", | |
50842bdc PP |
2031 | trace, bt_trace_get_name(trace), |
2032 | bt_byte_order_string(byte_order)); | |
dc3fffef | 2033 | |
bc37ae52 JG |
2034 | end: |
2035 | return ret; | |
2036 | } | |
2037 | ||
50842bdc PP |
2038 | struct bt_field_type *bt_trace_get_packet_header_type( |
2039 | struct bt_trace *trace) | |
d246b111 | 2040 | { |
50842bdc | 2041 | struct bt_field_type *field_type = NULL; |
d246b111 JG |
2042 | |
2043 | if (!trace) { | |
95c09b3a | 2044 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
d246b111 JG |
2045 | goto end; |
2046 | } | |
2047 | ||
83509119 | 2048 | bt_get(trace->packet_header_type); |
d246b111 JG |
2049 | field_type = trace->packet_header_type; |
2050 | end: | |
2051 | return field_type; | |
2052 | } | |
2053 | ||
50842bdc PP |
2054 | int bt_trace_set_packet_header_type(struct bt_trace *trace, |
2055 | struct bt_field_type *packet_header_type) | |
d246b111 JG |
2056 | { |
2057 | int ret = 0; | |
2058 | ||
95c09b3a PP |
2059 | if (!trace) { |
2060 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
2061 | ret = -1; | |
2062 | goto end; | |
2063 | } | |
2064 | ||
2065 | if (trace->frozen) { | |
2066 | BT_LOGW("Invalid parameter: trace is frozen: " | |
2067 | "addr=%p, name=\"%s\"", | |
50842bdc | 2068 | trace, bt_trace_get_name(trace)); |
d246b111 JG |
2069 | ret = -1; |
2070 | goto end; | |
2071 | } | |
2072 | ||
835b2d10 JG |
2073 | /* packet_header_type must be a structure. */ |
2074 | if (packet_header_type && | |
50842bdc | 2075 | !bt_field_type_is_structure(packet_header_type)) { |
95c09b3a PP |
2076 | BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: " |
2077 | "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s", | |
50842bdc | 2078 | trace, bt_trace_get_name(trace), |
95c09b3a | 2079 | packet_header_type, |
50842bdc | 2080 | bt_field_type_id_string(packet_header_type->id)); |
d246b111 JG |
2081 | ret = -1; |
2082 | goto end; | |
2083 | } | |
2084 | ||
83509119 | 2085 | bt_put(trace->packet_header_type); |
835b2d10 | 2086 | trace->packet_header_type = bt_get(packet_header_type); |
95c09b3a PP |
2087 | BT_LOGV("Set trace's packet header field type: " |
2088 | "addr=%p, name=\"%s\", packet-context-ft-addr=%p", | |
50842bdc | 2089 | trace, bt_trace_get_name(trace), packet_header_type); |
d246b111 JG |
2090 | end: |
2091 | return ret; | |
2092 | } | |
2093 | ||
8bf65fbd | 2094 | static |
544d0515 | 2095 | int64_t get_stream_class_count(void *element) |
8bf65fbd | 2096 | { |
50842bdc PP |
2097 | return bt_trace_get_stream_class_count( |
2098 | (struct bt_trace *) element); | |
8bf65fbd JG |
2099 | } |
2100 | ||
2101 | static | |
2102 | void *get_stream_class(void *element, int i) | |
2103 | { | |
50842bdc PP |
2104 | return bt_trace_get_stream_class_by_index( |
2105 | (struct bt_trace *) element, i); | |
8bf65fbd JG |
2106 | } |
2107 | ||
2108 | static | |
50842bdc | 2109 | int visit_stream_class(void *object, bt_visitor visitor,void *data) |
8bf65fbd | 2110 | { |
50842bdc | 2111 | return bt_stream_class_visit(object, visitor, data); |
8bf65fbd JG |
2112 | } |
2113 | ||
50842bdc PP |
2114 | int bt_trace_visit(struct bt_trace *trace, |
2115 | bt_visitor visitor, void *data) | |
8bf65fbd JG |
2116 | { |
2117 | int ret; | |
50842bdc PP |
2118 | struct bt_visitor_object obj = { |
2119 | .object = trace, | |
2120 | .type = BT_VISITOR_OBJECT_TYPE_TRACE | |
2121 | }; | |
8bf65fbd | 2122 | |
95c09b3a PP |
2123 | if (!trace) { |
2124 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
2125 | ret = -1; | |
2126 | goto end; | |
2127 | } | |
2128 | ||
2129 | if (!visitor) { | |
2130 | BT_LOGW_STR("Invalid parameter: visitor is NULL."); | |
8bf65fbd JG |
2131 | ret = -1; |
2132 | goto end; | |
2133 | } | |
2134 | ||
95c09b3a | 2135 | BT_LOGV("Visiting trace: addr=%p, name=\"%s\"", |
50842bdc | 2136 | trace, bt_trace_get_name(trace)); |
d9a13d86 | 2137 | ret = visitor_helper(&obj, get_stream_class_count, |
8bf65fbd JG |
2138 | get_stream_class, visit_stream_class, visitor, data); |
2139 | end: | |
2140 | return ret; | |
2141 | } | |
2142 | ||
2143 | static | |
50842bdc | 2144 | int invoke_listener(struct bt_visitor_object *object, void *data) |
8bf65fbd | 2145 | { |
9b888ff3 | 2146 | struct listener_wrapper *listener_wrapper = data; |
8bf65fbd | 2147 | |
d9a13d86 | 2148 | listener_wrapper->listener(object, listener_wrapper->data); |
9b888ff3 | 2149 | return 0; |
8bf65fbd JG |
2150 | } |
2151 | ||
95c09b3a | 2152 | // TODO: add logging to this function once we use it internally. |
50842bdc PP |
2153 | int bt_trace_add_listener(struct bt_trace *trace, |
2154 | bt_listener_cb listener, void *listener_data) | |
2204c381 JG |
2155 | { |
2156 | int ret = 0; | |
50ad4244 JG |
2157 | struct listener_wrapper *listener_wrapper = |
2158 | g_new0(struct listener_wrapper, 1); | |
2204c381 | 2159 | |
50ad4244 | 2160 | if (!trace || !listener || !listener_wrapper) { |
2204c381 JG |
2161 | ret = -1; |
2162 | goto error; | |
2163 | } | |
2164 | ||
50ad4244 JG |
2165 | listener_wrapper->listener = listener; |
2166 | listener_wrapper->data = listener_data; | |
8bf65fbd | 2167 | |
50ad4244 | 2168 | /* Visit the current schema. */ |
50842bdc | 2169 | ret = bt_trace_visit(trace, invoke_listener, listener_wrapper); |
8bf65fbd JG |
2170 | if (ret) { |
2171 | goto error; | |
2172 | } | |
2173 | ||
50ad4244 JG |
2174 | /* |
2175 | * Add listener to the array of callbacks which will be invoked on | |
2176 | * schema changes. | |
2177 | */ | |
2178 | g_ptr_array_add(trace->listeners, listener_wrapper); | |
2204c381 JG |
2179 | return ret; |
2180 | error: | |
50ad4244 | 2181 | g_free(listener_wrapper); |
2204c381 JG |
2182 | return ret; |
2183 | } | |
2184 | ||
bc37ae52 | 2185 | BT_HIDDEN |
50842bdc | 2186 | int bt_trace_object_modification(struct bt_visitor_object *object, |
9b888ff3 JG |
2187 | void *trace_ptr) |
2188 | { | |
2189 | size_t i; | |
50842bdc | 2190 | struct bt_trace *trace = trace_ptr; |
9b888ff3 JG |
2191 | |
2192 | assert(trace); | |
d9a13d86 | 2193 | assert(object); |
9b888ff3 JG |
2194 | |
2195 | if (trace->listeners->len == 0) { | |
2196 | goto end; | |
2197 | } | |
2198 | ||
2199 | for (i = 0; i < trace->listeners->len; i++) { | |
2200 | struct listener_wrapper *listener = | |
2201 | g_ptr_array_index(trace->listeners, i); | |
2202 | ||
d9a13d86 | 2203 | listener->listener(object, listener->data); |
9b888ff3 JG |
2204 | } |
2205 | end: | |
2206 | return 0; | |
2207 | } | |
2208 | ||
2209 | BT_HIDDEN | |
50842bdc | 2210 | struct bt_field_type *get_field_type(enum field_type_alias alias) |
bc37ae52 | 2211 | { |
a25e0d14 | 2212 | int ret; |
bc37ae52 | 2213 | unsigned int alignment, size; |
50842bdc | 2214 | struct bt_field_type *field_type = NULL; |
bc37ae52 JG |
2215 | |
2216 | if (alias >= NR_FIELD_TYPE_ALIAS) { | |
8bdcb82e | 2217 | goto end; |
bc37ae52 JG |
2218 | } |
2219 | ||
2220 | alignment = field_type_aliases_alignments[alias]; | |
2221 | size = field_type_aliases_sizes[alias]; | |
50842bdc PP |
2222 | field_type = bt_field_type_integer_create(size); |
2223 | ret = bt_field_type_set_alignment(field_type, alignment); | |
a25e0d14 JG |
2224 | if (ret) { |
2225 | BT_PUT(field_type); | |
2226 | } | |
8bdcb82e | 2227 | end: |
bc37ae52 JG |
2228 | return field_type; |
2229 | } | |
2230 | ||
f67f3a6e | 2231 | static |
50842bdc | 2232 | void bt_trace_freeze(struct bt_trace *trace) |
f67f3a6e | 2233 | { |
6474e016 PP |
2234 | int i; |
2235 | ||
95c09b3a PP |
2236 | if (trace->frozen) { |
2237 | return; | |
2238 | } | |
2239 | ||
2240 | BT_LOGD("Freezing trace: addr=%p, name=\"%s\"", | |
50842bdc | 2241 | trace, bt_trace_get_name(trace)); |
95c09b3a | 2242 | BT_LOGD_STR("Freezing packet header field type."); |
50842bdc | 2243 | bt_field_type_freeze(trace->packet_header_type); |
95c09b3a | 2244 | BT_LOGD_STR("Freezing environment attributes."); |
50842bdc | 2245 | bt_attributes_freeze(trace->environment); |
6474e016 | 2246 | |
95c09b3a PP |
2247 | if (trace->clocks->len > 0) { |
2248 | BT_LOGD_STR("Freezing clock classes."); | |
2249 | } | |
2250 | ||
6474e016 | 2251 | for (i = 0; i < trace->clocks->len; i++) { |
50842bdc | 2252 | struct bt_clock_class *clock_class = |
6474e016 PP |
2253 | g_ptr_array_index(trace->clocks, i); |
2254 | ||
50842bdc | 2255 | bt_clock_class_freeze(clock_class); |
6474e016 PP |
2256 | } |
2257 | ||
f67f3a6e JG |
2258 | trace->frozen = 1; |
2259 | } | |
2260 | ||
50842bdc | 2261 | bt_bool bt_trace_is_static(struct bt_trace *trace) |
5acf2ae6 | 2262 | { |
c55a9f58 | 2263 | bt_bool is_static = BT_FALSE; |
5acf2ae6 PP |
2264 | |
2265 | if (!trace) { | |
95c09b3a | 2266 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
5acf2ae6 PP |
2267 | goto end; |
2268 | } | |
2269 | ||
2270 | is_static = trace->is_static; | |
2271 | ||
2272 | end: | |
2273 | return is_static; | |
2274 | } | |
2275 | ||
50842bdc | 2276 | int bt_trace_set_is_static(struct bt_trace *trace) |
5acf2ae6 PP |
2277 | { |
2278 | int ret = 0; | |
3602afb0 | 2279 | size_t i; |
5acf2ae6 PP |
2280 | |
2281 | if (!trace) { | |
95c09b3a | 2282 | BT_LOGW_STR("Invalid parameter: trace is NULL."); |
5acf2ae6 PP |
2283 | ret = -1; |
2284 | goto end; | |
2285 | } | |
2286 | ||
726106d3 PP |
2287 | ret = check_packet_header_type_has_no_clock_class(trace); |
2288 | if (ret) { | |
2289 | /* check_packet_header_type_has_no_clock_class() logs errors */ | |
2290 | goto end; | |
2291 | } | |
2292 | ||
c55a9f58 | 2293 | trace->is_static = BT_TRUE; |
50842bdc | 2294 | bt_trace_freeze(trace); |
95c09b3a | 2295 | BT_LOGV("Set trace static: addr=%p, name=\"%s\"", |
50842bdc | 2296 | trace, bt_trace_get_name(trace)); |
5acf2ae6 | 2297 | |
3602afb0 PP |
2298 | /* Call all the "trace is static" listeners */ |
2299 | for (i = 0; i < trace->is_static_listeners->len; i++) { | |
50842bdc | 2300 | struct bt_trace_is_static_listener_elem elem = |
3602afb0 | 2301 | g_array_index(trace->is_static_listeners, |
50842bdc | 2302 | struct bt_trace_is_static_listener_elem, i); |
3602afb0 PP |
2303 | |
2304 | if (elem.func) { | |
2305 | elem.func(trace, elem.data); | |
2306 | } | |
2307 | } | |
2308 | ||
2309 | end: | |
2310 | return ret; | |
2311 | } | |
2312 | ||
50842bdc PP |
2313 | int bt_trace_add_is_static_listener(struct bt_trace *trace, |
2314 | bt_trace_is_static_listener listener, | |
2315 | bt_trace_listener_removed listener_removed, void *data) | |
3602afb0 PP |
2316 | { |
2317 | int i; | |
50842bdc | 2318 | struct bt_trace_is_static_listener_elem new_elem = { |
3602afb0 | 2319 | .func = listener, |
8480c8cc | 2320 | .removed = listener_removed, |
3602afb0 PP |
2321 | .data = data, |
2322 | }; | |
2323 | ||
2324 | if (!trace) { | |
2325 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
2326 | i = -1; | |
2327 | goto end; | |
2328 | } | |
2329 | ||
2330 | if (!listener) { | |
2331 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
2332 | i = -1; | |
2333 | goto end; | |
2334 | } | |
2335 | ||
2336 | if (trace->is_static) { | |
2337 | BT_LOGW("Invalid parameter: trace is already static: " | |
2338 | "addr=%p, name=\"%s\"", | |
50842bdc | 2339 | trace, bt_trace_get_name(trace)); |
3602afb0 PP |
2340 | i = -1; |
2341 | goto end; | |
2342 | } | |
2343 | ||
8480c8cc PP |
2344 | if (trace->in_remove_listener) { |
2345 | BT_LOGW("Cannot call this function during the execution of a remove listener: " | |
2346 | "addr=%p, name=\"%s\"", | |
50842bdc | 2347 | trace, bt_trace_get_name(trace)); |
8480c8cc PP |
2348 | i = -1; |
2349 | goto end; | |
2350 | } | |
2351 | ||
3602afb0 PP |
2352 | /* Find the next available spot */ |
2353 | for (i = 0; i < trace->is_static_listeners->len; i++) { | |
50842bdc | 2354 | struct bt_trace_is_static_listener_elem elem = |
3602afb0 | 2355 | g_array_index(trace->is_static_listeners, |
50842bdc | 2356 | struct bt_trace_is_static_listener_elem, i); |
3602afb0 PP |
2357 | |
2358 | if (!elem.func) { | |
2359 | break; | |
2360 | } | |
2361 | } | |
2362 | ||
2363 | if (i == trace->is_static_listeners->len) { | |
2364 | g_array_append_val(trace->is_static_listeners, new_elem); | |
2365 | } else { | |
2366 | g_array_insert_val(trace->is_static_listeners, i, new_elem); | |
2367 | } | |
2368 | ||
2369 | BT_LOGV("Added \"trace is static\" listener: " | |
2370 | "trace-addr=%p, trace-name=\"%s\", func-addr=%p, " | |
2371 | "data-addr=%p, listener-id=%d", | |
50842bdc | 2372 | trace, bt_trace_get_name(trace), listener, data, i); |
3602afb0 PP |
2373 | |
2374 | end: | |
2375 | return i; | |
2376 | } | |
2377 | ||
50842bdc PP |
2378 | int bt_trace_remove_is_static_listener( |
2379 | struct bt_trace *trace, int listener_id) | |
3602afb0 PP |
2380 | { |
2381 | int ret = 0; | |
50842bdc | 2382 | struct bt_trace_is_static_listener_elem *elem; |
3602afb0 PP |
2383 | |
2384 | if (!trace) { | |
2385 | BT_LOGW_STR("Invalid parameter: trace is NULL."); | |
2386 | ret = -1; | |
2387 | goto end; | |
2388 | } | |
2389 | ||
8480c8cc PP |
2390 | if (trace->in_remove_listener) { |
2391 | BT_LOGW("Cannot call this function during the execution of a remove listener: " | |
2392 | "addr=%p, name=\"%s\", listener-id=%d", | |
50842bdc | 2393 | trace, bt_trace_get_name(trace), |
8480c8cc PP |
2394 | listener_id); |
2395 | ret = -1; | |
2396 | goto end; | |
2397 | } | |
2398 | ||
3602afb0 PP |
2399 | if (listener_id < 0) { |
2400 | BT_LOGW("Invalid listener ID: must be zero or positive: " | |
2401 | "listener-id=%d", listener_id); | |
2402 | ret = -1; | |
2403 | goto end; | |
2404 | } | |
2405 | ||
2406 | if (listener_id >= trace->is_static_listeners->len) { | |
2407 | BT_LOGW("Invalid parameter: no listener with this listener ID: " | |
2408 | "addr=%p, name=\"%s\", listener-id=%d", | |
50842bdc | 2409 | trace, bt_trace_get_name(trace), |
3602afb0 PP |
2410 | listener_id); |
2411 | ret = -1; | |
2412 | goto end; | |
2413 | } | |
2414 | ||
2415 | elem = &g_array_index(trace->is_static_listeners, | |
50842bdc | 2416 | struct bt_trace_is_static_listener_elem, |
3602afb0 PP |
2417 | listener_id); |
2418 | if (!elem->func) { | |
2419 | BT_LOGW("Invalid parameter: no listener with this listener ID: " | |
2420 | "addr=%p, name=\"%s\", listener-id=%d", | |
50842bdc | 2421 | trace, bt_trace_get_name(trace), |
3602afb0 PP |
2422 | listener_id); |
2423 | ret = -1; | |
2424 | goto end; | |
2425 | } | |
2426 | ||
8480c8cc PP |
2427 | if (elem->removed) { |
2428 | /* Call remove listener */ | |
2429 | BT_LOGV("Calling remove listener: " | |
2430 | "trace-addr=%p, trace-name=\"%s\", " | |
50842bdc | 2431 | "listener-id=%d", trace, bt_trace_get_name(trace), |
8480c8cc PP |
2432 | listener_id); |
2433 | trace->in_remove_listener = BT_TRUE; | |
2434 | elem->removed(trace, elem->data); | |
2435 | trace->in_remove_listener = BT_FALSE; | |
2436 | } | |
2437 | ||
3602afb0 | 2438 | elem->func = NULL; |
8480c8cc | 2439 | elem->removed = NULL; |
3602afb0 PP |
2440 | elem->data = NULL; |
2441 | BT_LOGV("Removed \"trace is static\" listener: " | |
2442 | "trace-addr=%p, trace-name=\"%s\", " | |
50842bdc | 2443 | "listener-id=%d", trace, bt_trace_get_name(trace), |
3602afb0 PP |
2444 | listener_id); |
2445 | ||
5acf2ae6 PP |
2446 | end: |
2447 | return ret; | |
2448 | } |