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