Commit | Line | Data |
---|---|---|
9843982d | 1 | /* |
5c5facc7 | 2 | * ctf/events.c |
9843982d JD |
3 | * |
4 | * Babeltrace Library | |
5 | * | |
6 | * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation | |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
9 | * Julien Desfossez <julien.desfossez@efficios.com> | |
10 | * | |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | */ | |
21 | ||
22 | #include <babeltrace/babeltrace.h> | |
23 | #include <babeltrace/format.h> | |
24 | #include <babeltrace/ctf/events.h> | |
25 | #include <babeltrace/ctf-ir/metadata.h> | |
e4195791 MD |
26 | #include <babeltrace/prio_heap.h> |
27 | #include <babeltrace/iterator-internal.h> | |
28 | #include <babeltrace/ctf/events-internal.h> | |
29 | #include <babeltrace/ctf/metadata.h> | |
9843982d JD |
30 | #include <glib.h> |
31 | ||
c34ea0fa MD |
32 | #include "events-private.h" |
33 | ||
9843982d JD |
34 | /* |
35 | * thread local storage to store the last error that occured | |
36 | * while reading a field, this variable must be accessed by | |
b330165c | 37 | * bt_ctf_field_get_error only |
9843982d JD |
38 | */ |
39 | __thread int bt_ctf_last_field_error = 0; | |
40 | ||
c50d2a7a | 41 | const struct definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event, |
9843982d JD |
42 | enum bt_ctf_scope scope) |
43 | { | |
554cf424 MD |
44 | const struct definition *tmp = NULL; |
45 | const struct ctf_event_definition *event; | |
9843982d | 46 | |
7f89ddce MD |
47 | if (!ctf_event) |
48 | return NULL; | |
49 | ||
50 | event = ctf_event->parent; | |
9843982d JD |
51 | switch (scope) { |
52 | case BT_TRACE_PACKET_HEADER: | |
53 | if (!event->stream) | |
54 | goto error; | |
55 | if (event->stream->trace_packet_header) | |
56 | tmp = &event->stream->trace_packet_header->p; | |
57 | break; | |
58 | case BT_STREAM_PACKET_CONTEXT: | |
59 | if (!event->stream) | |
60 | goto error; | |
61 | if (event->stream->stream_packet_context) | |
62 | tmp = &event->stream->stream_packet_context->p; | |
63 | break; | |
64 | case BT_STREAM_EVENT_HEADER: | |
65 | if (!event->stream) | |
66 | goto error; | |
67 | if (event->stream->stream_event_header) | |
68 | tmp = &event->stream->stream_event_header->p; | |
69 | break; | |
70 | case BT_STREAM_EVENT_CONTEXT: | |
71 | if (!event->stream) | |
72 | goto error; | |
73 | if (event->stream->stream_event_context) | |
74 | tmp = &event->stream->stream_event_context->p; | |
75 | break; | |
76 | case BT_EVENT_CONTEXT: | |
8a4722b0 JD |
77 | if (event->event_context) |
78 | tmp = &event->event_context->p; | |
9843982d JD |
79 | break; |
80 | case BT_EVENT_FIELDS: | |
8a4722b0 JD |
81 | if (event->event_fields) |
82 | tmp = &event->event_fields->p; | |
9843982d JD |
83 | break; |
84 | } | |
85 | return tmp; | |
86 | ||
87 | error: | |
88 | return NULL; | |
89 | } | |
90 | ||
c50d2a7a | 91 | const struct definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event, |
04ae3991 | 92 | const struct definition *scope, |
9843982d JD |
93 | const char *field) |
94 | { | |
554cf424 | 95 | const struct definition *def; |
300d317a | 96 | char *field_underscore; |
9843982d | 97 | |
7f89ddce MD |
98 | if (!ctf_event || !scope || !field) |
99 | return NULL; | |
100 | ||
101 | def = lookup_definition(scope, field); | |
102 | /* | |
103 | * optionally a field can have an underscore prefix, try | |
104 | * to lookup the field with this prefix if it failed | |
105 | */ | |
106 | if (!def) { | |
107 | field_underscore = g_new(char, strlen(field) + 2); | |
108 | field_underscore[0] = '_'; | |
109 | strcpy(&field_underscore[1], field); | |
110 | def = lookup_definition(scope, field_underscore); | |
111 | g_free(field_underscore); | |
9843982d | 112 | } |
b14d90be | 113 | if (bt_ctf_field_type(bt_ctf_get_decl_from_def(def)) == CTF_TYPE_VARIANT) { |
554cf424 | 114 | const struct definition_variant *variant_definition; |
7f89ddce | 115 | variant_definition = container_of(def, |
554cf424 | 116 | const struct definition_variant, p); |
7f89ddce MD |
117 | return variant_definition->current_field; |
118 | } | |
119 | return def; | |
9843982d JD |
120 | } |
121 | ||
c50d2a7a | 122 | const struct definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event, |
04ae3991 | 123 | const struct definition *field, |
9843982d JD |
124 | unsigned int index) |
125 | { | |
126 | struct definition *ret = NULL; | |
127 | ||
7f89ddce MD |
128 | if (!ctf_event || !field) |
129 | return NULL; | |
130 | ||
b14d90be | 131 | if (bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_ARRAY) { |
9843982d JD |
132 | struct definition_array *array_definition; |
133 | array_definition = container_of(field, | |
134 | struct definition_array, p); | |
135 | ret = array_index(array_definition, index); | |
b14d90be | 136 | } else if (bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_SEQUENCE) { |
9843982d JD |
137 | struct definition_sequence *sequence_definition; |
138 | sequence_definition = container_of(field, | |
139 | struct definition_sequence, p); | |
140 | ret = sequence_index(sequence_definition, index); | |
141 | } | |
142 | return ret; | |
143 | } | |
144 | ||
c50d2a7a | 145 | const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event) |
9843982d | 146 | { |
554cf424 MD |
147 | const struct ctf_event_declaration *event_class; |
148 | const struct ctf_stream_declaration *stream_class; | |
149 | const struct ctf_event_definition *event; | |
9843982d | 150 | |
7f89ddce | 151 | if (!ctf_event) |
9843982d | 152 | return NULL; |
7f89ddce MD |
153 | |
154 | event = ctf_event->parent; | |
9843982d JD |
155 | stream_class = event->stream->stream_class; |
156 | event_class = g_ptr_array_index(stream_class->events_by_id, | |
157 | event->stream->event_id); | |
158 | return g_quark_to_string(event_class->name); | |
159 | } | |
160 | ||
161 | const char *bt_ctf_field_name(const struct definition *def) | |
162 | { | |
7f89ddce MD |
163 | if (!def) |
164 | return NULL; | |
165 | ||
166 | return rem_(g_quark_to_string(def->name)); | |
9843982d JD |
167 | } |
168 | ||
2bdfa4cf | 169 | enum ctf_type_id bt_ctf_field_type(const struct declaration *decl) |
9843982d | 170 | { |
2bdfa4cf | 171 | if (!decl) |
7f89ddce MD |
172 | return CTF_TYPE_UNKNOWN; |
173 | ||
2bdfa4cf | 174 | return decl->id; |
9843982d JD |
175 | } |
176 | ||
c50d2a7a | 177 | int bt_ctf_get_field_list(const struct bt_ctf_event *ctf_event, |
04ae3991 | 178 | const struct definition *scope, |
9843982d JD |
179 | struct definition const * const **list, |
180 | unsigned int *count) | |
181 | { | |
7f89ddce MD |
182 | if (!ctf_event || !scope || !list || !count) |
183 | return -EINVAL; | |
184 | ||
b14d90be | 185 | switch (bt_ctf_field_type(bt_ctf_get_decl_from_def(scope))) { |
9843982d JD |
186 | case CTF_TYPE_INTEGER: |
187 | case CTF_TYPE_FLOAT: | |
188 | case CTF_TYPE_STRING: | |
189 | case CTF_TYPE_ENUM: | |
190 | goto error; | |
191 | case CTF_TYPE_STRUCT: | |
192 | { | |
04ae3991 | 193 | const struct definition_struct *def_struct; |
9843982d | 194 | |
04ae3991 | 195 | def_struct = container_of(scope, const struct definition_struct, p); |
9843982d JD |
196 | if (!def_struct) |
197 | goto error; | |
198 | if (def_struct->fields->pdata) { | |
199 | *list = (struct definition const* const*) def_struct->fields->pdata; | |
200 | *count = def_struct->fields->len; | |
201 | goto end; | |
202 | } else { | |
203 | goto error; | |
204 | } | |
7f89ddce | 205 | break; |
9843982d JD |
206 | } |
207 | case CTF_TYPE_UNTAGGED_VARIANT: | |
208 | goto error; | |
209 | case CTF_TYPE_VARIANT: | |
210 | { | |
04ae3991 | 211 | const struct definition_variant *def_variant; |
9843982d | 212 | |
04ae3991 | 213 | def_variant = container_of(scope, const struct definition_variant, p); |
9843982d JD |
214 | if (!def_variant) |
215 | goto error; | |
216 | if (def_variant->fields->pdata) { | |
217 | *list = (struct definition const* const*) def_variant->fields->pdata; | |
218 | *count = def_variant->fields->len; | |
219 | goto end; | |
220 | } else { | |
221 | goto error; | |
222 | } | |
7f89ddce | 223 | break; |
9843982d JD |
224 | } |
225 | case CTF_TYPE_ARRAY: | |
226 | { | |
04ae3991 | 227 | const struct definition_array *def_array; |
9843982d | 228 | |
04ae3991 | 229 | def_array = container_of(scope, const struct definition_array, p); |
9843982d JD |
230 | if (!def_array) |
231 | goto error; | |
232 | if (def_array->elems->pdata) { | |
233 | *list = (struct definition const* const*) def_array->elems->pdata; | |
234 | *count = def_array->elems->len; | |
235 | goto end; | |
236 | } else { | |
237 | goto error; | |
238 | } | |
7f89ddce | 239 | break; |
9843982d JD |
240 | } |
241 | case CTF_TYPE_SEQUENCE: | |
242 | { | |
04ae3991 | 243 | const struct definition_sequence *def_sequence; |
9843982d | 244 | |
04ae3991 | 245 | def_sequence = container_of(scope, const struct definition_sequence, p); |
9843982d JD |
246 | if (!def_sequence) |
247 | goto error; | |
248 | if (def_sequence->elems->pdata) { | |
249 | *list = (struct definition const* const*) def_sequence->elems->pdata; | |
250 | *count = def_sequence->elems->len; | |
251 | goto end; | |
252 | } else { | |
253 | goto error; | |
254 | } | |
7f89ddce | 255 | break; |
9843982d JD |
256 | } |
257 | default: | |
258 | break; | |
259 | } | |
260 | ||
261 | end: | |
262 | return 0; | |
263 | ||
264 | error: | |
265 | *list = NULL; | |
266 | *count = 0; | |
267 | return -1; | |
268 | } | |
269 | ||
c50d2a7a | 270 | struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *ctf_event) |
98a04903 JD |
271 | { |
272 | struct bt_context *ret = NULL; | |
554cf424 MD |
273 | const struct ctf_file_stream *cfs; |
274 | const struct ctf_trace *trace; | |
275 | const struct ctf_event_definition *event; | |
98a04903 | 276 | |
7f89ddce MD |
277 | if (!ctf_event) |
278 | return NULL; | |
279 | ||
280 | event = ctf_event->parent; | |
554cf424 | 281 | cfs = container_of(event->stream, const struct ctf_file_stream, |
98a04903 | 282 | parent); |
8b8dc96e | 283 | trace = cfs->parent.stream_class->trace; |
98a04903 JD |
284 | if (trace->ctx) |
285 | ret = trace->ctx; | |
286 | ||
287 | return ret; | |
288 | } | |
289 | ||
c50d2a7a | 290 | int bt_ctf_event_get_handle_id(const struct bt_ctf_event *ctf_event) |
98a04903 JD |
291 | { |
292 | int ret = -1; | |
554cf424 MD |
293 | const struct ctf_file_stream *cfs; |
294 | const struct ctf_trace *trace; | |
295 | const struct ctf_event_definition *event; | |
7f89ddce MD |
296 | |
297 | if (!ctf_event) | |
298 | return -EINVAL; | |
98a04903 | 299 | |
7f89ddce | 300 | event = ctf_event->parent; |
554cf424 | 301 | cfs = container_of(event->stream, const struct ctf_file_stream, |
98a04903 | 302 | parent); |
8b8dc96e | 303 | trace = cfs->parent.stream_class->trace; |
98a04903 JD |
304 | if (trace->handle) |
305 | ret = trace->handle->id; | |
306 | ||
307 | return ret; | |
308 | } | |
309 | ||
d40ee8ec | 310 | uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event) |
9843982d | 311 | { |
554cf424 | 312 | const struct ctf_event_definition *event; |
7f89ddce MD |
313 | |
314 | if (!ctf_event) | |
315 | return -1ULL; | |
316 | ||
317 | event = ctf_event->parent; | |
9843982d | 318 | if (event && event->stream->has_timestamp) |
03798a93 | 319 | return event->stream->real_timestamp; |
9843982d | 320 | else |
57f3005e SJD |
321 | return -1ULL; |
322 | } | |
323 | ||
d40ee8ec | 324 | uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event) |
57f3005e | 325 | { |
554cf424 | 326 | const struct ctf_event_definition *event; |
7f89ddce MD |
327 | |
328 | if (!ctf_event) | |
329 | return -1ULL; | |
330 | ||
331 | event = ctf_event->parent; | |
c34ea0fa | 332 | if (event && event->stream->has_timestamp) |
03798a93 | 333 | return event->stream->cycles_timestamp; |
c34ea0fa | 334 | else |
57f3005e | 335 | return -1ULL; |
9843982d JD |
336 | } |
337 | ||
338 | static void bt_ctf_field_set_error(int error) | |
339 | { | |
340 | bt_ctf_last_field_error = error; | |
341 | } | |
342 | ||
343 | int bt_ctf_field_get_error(void) | |
344 | { | |
345 | int ret; | |
346 | ret = bt_ctf_last_field_error; | |
347 | bt_ctf_last_field_error = 0; | |
348 | ||
349 | return ret; | |
350 | } | |
351 | ||
554cf424 MD |
352 | static const struct declaration_integer * |
353 | get_declaration_integer(const struct declaration *decl) | |
2bdfa4cf | 354 | { |
554cf424 MD |
355 | if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_INTEGER) |
356 | return NULL; | |
357 | return container_of(decl, const struct declaration_integer, p); | |
2bdfa4cf JD |
358 | } |
359 | ||
554cf424 MD |
360 | static const struct declaration_string * |
361 | get_declaration_string(const struct declaration *decl) | |
2bdfa4cf | 362 | { |
554cf424 MD |
363 | if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_STRING) |
364 | return NULL; | |
365 | return container_of(decl, const struct declaration_string, p); | |
2bdfa4cf JD |
366 | } |
367 | ||
554cf424 MD |
368 | static const struct declaration_array * |
369 | get_declaration_array(const struct declaration *decl) | |
2bdfa4cf | 370 | { |
554cf424 MD |
371 | if (!decl || bt_ctf_field_type(decl) != CTF_TYPE_ARRAY) |
372 | return NULL; | |
373 | return container_of(decl, const struct declaration_array, p); | |
2bdfa4cf JD |
374 | } |
375 | ||
376 | int bt_ctf_get_int_signedness(const struct declaration *decl) | |
8673030f | 377 | { |
554cf424 | 378 | const struct declaration_integer *integer; |
8673030f | 379 | |
2bdfa4cf | 380 | integer = get_declaration_integer(decl); |
554cf424 | 381 | if (!integer) { |
8673030f | 382 | bt_ctf_field_set_error(-EINVAL); |
554cf424 | 383 | return -EINVAL; |
8673030f | 384 | } |
554cf424 | 385 | return integer->signedness; |
8673030f JD |
386 | } |
387 | ||
2bdfa4cf | 388 | int bt_ctf_get_int_base(const struct declaration *decl) |
8673030f | 389 | { |
554cf424 | 390 | const struct declaration_integer *integer; |
8673030f | 391 | |
2bdfa4cf | 392 | integer = get_declaration_integer(decl); |
554cf424 | 393 | if (!integer) { |
8673030f | 394 | bt_ctf_field_set_error(-EINVAL); |
554cf424 | 395 | return -EINVAL; |
8673030f | 396 | } |
554cf424 | 397 | return integer->base; |
8673030f JD |
398 | } |
399 | ||
2bdfa4cf | 400 | int bt_ctf_get_int_byte_order(const struct declaration *decl) |
8673030f | 401 | { |
554cf424 | 402 | const struct declaration_integer *integer; |
8673030f | 403 | |
2bdfa4cf | 404 | integer = get_declaration_integer(decl); |
554cf424 | 405 | if (!integer) { |
8673030f | 406 | bt_ctf_field_set_error(-EINVAL); |
554cf424 | 407 | return -EINVAL; |
8673030f | 408 | } |
554cf424 | 409 | return integer->byte_order; |
8673030f JD |
410 | } |
411 | ||
2bdfa4cf | 412 | ssize_t bt_ctf_get_int_len(const struct declaration *decl) |
fef0e521 | 413 | { |
554cf424 | 414 | const struct declaration_integer *integer; |
fef0e521 | 415 | |
2bdfa4cf | 416 | integer = get_declaration_integer(decl); |
554cf424 | 417 | if (!integer) { |
fef0e521 | 418 | bt_ctf_field_set_error(-EINVAL); |
554cf424 | 419 | return -EINVAL; |
fef0e521 | 420 | } |
554cf424 | 421 | return (ssize_t) integer->len; |
fef0e521 MD |
422 | } |
423 | ||
4d25c350 MD |
424 | const struct definition *bt_ctf_get_enum_int(const struct definition *field) |
425 | { | |
554cf424 | 426 | const struct definition_enum *def_enum; |
4d25c350 | 427 | |
b14d90be | 428 | if (!field || bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) != CTF_TYPE_ENUM) { |
4d25c350 MD |
429 | bt_ctf_field_set_error(-EINVAL); |
430 | return NULL; | |
431 | } | |
554cf424 | 432 | def_enum = container_of(field, const struct definition_enum, p); |
4d25c350 MD |
433 | return &def_enum->integer->p; |
434 | } | |
435 | ||
436 | const char *bt_ctf_get_enum_str(const struct definition *field) | |
437 | { | |
554cf424 MD |
438 | const struct definition_enum *def_enum; |
439 | const struct declaration_enum *decl_enum; | |
4d25c350 MD |
440 | GArray *array; |
441 | const char *ret; | |
442 | ||
b14d90be | 443 | if (!field || bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) != CTF_TYPE_ENUM) { |
4d25c350 MD |
444 | bt_ctf_field_set_error(-EINVAL); |
445 | return NULL; | |
446 | } | |
554cf424 | 447 | def_enum = container_of(field, const struct definition_enum, p); |
4d25c350 MD |
448 | decl_enum = def_enum->declaration; |
449 | if (get_int_signedness(&def_enum->integer->p)) { | |
450 | array = enum_int_to_quark_set(decl_enum, | |
451 | get_signed_int(&def_enum->integer->p)); | |
452 | } else { | |
453 | array = enum_uint_to_quark_set(decl_enum, | |
454 | get_unsigned_int(&def_enum->integer->p)); | |
455 | } | |
456 | if (!array) { | |
457 | bt_ctf_field_set_error(-ENOENT); | |
458 | return NULL; | |
459 | } | |
460 | ||
461 | if (array->len == 0) { | |
462 | g_array_unref(array); | |
463 | bt_ctf_field_set_error(-ENOENT); | |
464 | return NULL; | |
465 | } | |
466 | /* Return first string. Arbitrary choice. */ | |
467 | ret = g_quark_to_string(g_array_index(array, GQuark, 0)); | |
468 | g_array_unref(array); | |
469 | return ret; | |
470 | } | |
471 | ||
2bdfa4cf | 472 | enum ctf_string_encoding bt_ctf_get_encoding(const struct declaration *decl) |
8673030f JD |
473 | { |
474 | enum ctf_string_encoding ret = 0; | |
554cf424 MD |
475 | const struct declaration_integer *integer; |
476 | const struct declaration_string *string; | |
8673030f | 477 | |
2bdfa4cf | 478 | if (!decl) |
7f89ddce | 479 | goto error; |
8673030f | 480 | |
2bdfa4cf JD |
481 | if (bt_ctf_field_type(decl) == CTF_TYPE_INTEGER) { |
482 | integer = get_declaration_integer(decl); | |
483 | if (integer) { | |
484 | ret = integer->encoding; | |
485 | } else { | |
486 | goto error; | |
487 | } | |
488 | } else if (bt_ctf_field_type(decl) == CTF_TYPE_STRING) { | |
489 | string = get_declaration_string(decl); | |
490 | if (string) { | |
491 | ret = string->encoding; | |
492 | } else { | |
493 | goto error; | |
494 | } | |
495 | } else { | |
8673030f | 496 | goto error; |
2bdfa4cf | 497 | } |
8673030f JD |
498 | return ret; |
499 | ||
500 | error: | |
501 | bt_ctf_field_set_error(-EINVAL); | |
502 | return -1; | |
503 | } | |
504 | ||
2bdfa4cf | 505 | int bt_ctf_get_array_len(const struct declaration *decl) |
8673030f | 506 | { |
554cf424 | 507 | const struct declaration_array *array; |
8673030f | 508 | |
554cf424 MD |
509 | array = get_declaration_array(decl); |
510 | if (!array) | |
2bdfa4cf | 511 | goto error; |
554cf424 | 512 | return array->len; |
2bdfa4cf JD |
513 | |
514 | error: | |
515 | bt_ctf_field_set_error(-EINVAL); | |
516 | return -1; | |
8673030f JD |
517 | } |
518 | ||
da320b83 | 519 | uint64_t bt_ctf_get_uint64(const struct definition *field) |
9843982d | 520 | { |
63dd3817 | 521 | uint64_t ret = 0; |
9843982d | 522 | |
b14d90be | 523 | if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_INTEGER) |
9843982d JD |
524 | ret = get_unsigned_int(field); |
525 | else | |
526 | bt_ctf_field_set_error(-EINVAL); | |
527 | ||
528 | return ret; | |
529 | } | |
530 | ||
da320b83 | 531 | int64_t bt_ctf_get_int64(const struct definition *field) |
9843982d | 532 | { |
63dd3817 | 533 | int64_t ret = 0; |
9843982d | 534 | |
b14d90be | 535 | if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_INTEGER) |
9843982d JD |
536 | ret = get_signed_int(field); |
537 | else | |
538 | bt_ctf_field_set_error(-EINVAL); | |
539 | ||
540 | return ret; | |
9843982d JD |
541 | } |
542 | ||
da320b83 | 543 | char *bt_ctf_get_char_array(const struct definition *field) |
9843982d JD |
544 | { |
545 | char *ret = NULL; | |
885885bf | 546 | GString *char_array; |
9843982d | 547 | |
b14d90be | 548 | if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_ARRAY) { |
885885bf JD |
549 | char_array = get_char_array(field); |
550 | if (char_array) { | |
551 | ret = char_array->str; | |
552 | goto end; | |
553 | } | |
554 | } | |
555 | bt_ctf_field_set_error(-EINVAL); | |
9843982d | 556 | |
885885bf | 557 | end: |
9843982d JD |
558 | return ret; |
559 | } | |
560 | ||
da320b83 | 561 | char *bt_ctf_get_string(const struct definition *field) |
9843982d JD |
562 | { |
563 | char *ret = NULL; | |
564 | ||
b14d90be | 565 | if (field && bt_ctf_field_type(bt_ctf_get_decl_from_def(field)) == CTF_TYPE_STRING) |
9843982d JD |
566 | ret = get_string(field); |
567 | else | |
568 | bt_ctf_field_set_error(-EINVAL); | |
569 | ||
570 | return ret; | |
571 | } | |
e003ab50 JD |
572 | |
573 | int bt_ctf_get_event_decl_list(int handle_id, struct bt_context *ctx, | |
64c2c249 | 574 | struct bt_ctf_event_decl * const **list, |
e003ab50 JD |
575 | unsigned int *count) |
576 | { | |
577 | struct bt_trace_handle *handle; | |
578 | struct trace_descriptor *td; | |
579 | struct ctf_trace *tin; | |
580 | ||
7f89ddce | 581 | if (!ctx || !list || !count) |
e003ab50 JD |
582 | goto error; |
583 | ||
584 | handle = g_hash_table_lookup(ctx->trace_handles, | |
585 | (gpointer) (unsigned long) handle_id); | |
586 | if (!handle) | |
587 | goto error; | |
588 | ||
589 | td = handle->td; | |
590 | tin = container_of(td, struct ctf_trace, parent); | |
591 | ||
64c2c249 | 592 | *list = (struct bt_ctf_event_decl * const*) tin->event_declarations->pdata; |
e003ab50 JD |
593 | *count = tin->event_declarations->len; |
594 | return 0; | |
595 | ||
596 | error: | |
597 | return -1; | |
598 | } | |
599 | ||
600 | const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event) | |
601 | { | |
602 | if (!event) | |
603 | return NULL; | |
7f89ddce | 604 | |
e003ab50 JD |
605 | return g_quark_to_string(event->parent.name); |
606 | } | |
64c2c249 JD |
607 | |
608 | int bt_ctf_get_decl_fields(struct bt_ctf_event_decl *event_decl, | |
609 | enum bt_ctf_scope scope, | |
610 | struct bt_ctf_field_decl const * const **list, | |
611 | unsigned int *count) | |
612 | { | |
613 | int i; | |
614 | GArray *fields = NULL; | |
615 | gpointer *ret_list = NULL; | |
616 | GPtrArray *fields_array = NULL; | |
617 | int ret = 0; | |
618 | *count = 0; | |
619 | ||
7f89ddce MD |
620 | if (!event_decl || !list || !count) |
621 | return -EINVAL; | |
622 | ||
64c2c249 JD |
623 | switch (scope) { |
624 | case BT_EVENT_CONTEXT: | |
625 | if (event_decl->context_decl) { | |
626 | ret_list = event_decl->context_decl->pdata; | |
627 | *count = event_decl->context_decl->len; | |
628 | goto end; | |
629 | } | |
630 | event_decl->context_decl = g_ptr_array_new(); | |
631 | if (!event_decl->parent.context_decl) { | |
632 | ret = -1; | |
633 | goto end; | |
634 | } | |
635 | fields = event_decl->parent.context_decl->fields; | |
636 | fields_array = event_decl->context_decl; | |
637 | break; | |
638 | case BT_EVENT_FIELDS: | |
639 | if (event_decl->fields_decl) { | |
640 | ret_list = event_decl->fields_decl->pdata; | |
641 | *count = event_decl->fields_decl->len; | |
642 | goto end; | |
643 | } | |
644 | event_decl->fields_decl = g_ptr_array_new(); | |
645 | if (!event_decl->parent.fields_decl) { | |
646 | ret = -1; | |
647 | goto end; | |
648 | } | |
649 | fields = event_decl->parent.fields_decl->fields; | |
650 | fields_array = event_decl->fields_decl; | |
651 | break; | |
652 | case BT_STREAM_PACKET_CONTEXT: | |
653 | if (event_decl->packet_context_decl) { | |
654 | ret_list = event_decl->packet_context_decl->pdata; | |
655 | *count = event_decl->packet_context_decl->len; | |
656 | goto end; | |
657 | } | |
658 | event_decl->packet_context_decl = g_ptr_array_new(); | |
659 | if (!event_decl->parent.stream->packet_context_decl) { | |
660 | ret = -1; | |
661 | goto end; | |
662 | } | |
663 | fields = event_decl->parent.stream->packet_context_decl->fields; | |
664 | fields_array = event_decl->packet_context_decl; | |
665 | break; | |
666 | case BT_STREAM_EVENT_CONTEXT: | |
667 | if (event_decl->event_context_decl) { | |
668 | ret_list = event_decl->event_context_decl->pdata; | |
669 | *count = event_decl->event_context_decl->len; | |
670 | goto end; | |
671 | } | |
672 | event_decl->event_context_decl = g_ptr_array_new(); | |
673 | if (!event_decl->parent.stream->event_context_decl) { | |
674 | ret = -1; | |
675 | goto end; | |
676 | } | |
677 | fields = event_decl->parent.stream->event_context_decl->fields; | |
678 | fields_array = event_decl->event_context_decl; | |
679 | break; | |
680 | case BT_STREAM_EVENT_HEADER: | |
681 | if (event_decl->event_header_decl) { | |
682 | ret_list = event_decl->event_header_decl->pdata; | |
683 | *count = event_decl->event_header_decl->len; | |
684 | goto end; | |
685 | } | |
686 | event_decl->event_header_decl = g_ptr_array_new(); | |
687 | if (!event_decl->parent.stream->event_header_decl) { | |
688 | ret = -1; | |
689 | goto end; | |
690 | } | |
691 | fields = event_decl->parent.stream->event_header_decl->fields; | |
692 | fields_array = event_decl->event_header_decl; | |
693 | break; | |
694 | case BT_TRACE_PACKET_HEADER: | |
695 | if (event_decl->packet_header_decl) { | |
696 | ret_list = event_decl->packet_header_decl->pdata; | |
697 | *count = event_decl->packet_header_decl->len; | |
698 | goto end; | |
699 | } | |
700 | event_decl->packet_header_decl = g_ptr_array_new(); | |
701 | if (!event_decl->parent.stream->trace->packet_header_decl) { | |
702 | ret = -1; | |
703 | goto end; | |
704 | } | |
705 | fields = event_decl->parent.stream->trace->packet_header_decl->fields; | |
706 | fields_array = event_decl->packet_header_decl; | |
707 | break; | |
708 | } | |
709 | ||
710 | for (i = 0; i < fields->len; i++) { | |
711 | g_ptr_array_add(fields_array, | |
712 | &g_array_index(fields, | |
713 | struct declaration_field, i)); | |
714 | } | |
715 | ret_list = fields_array->pdata; | |
716 | *count = fields->len; | |
717 | ||
718 | end: | |
719 | *list = (struct bt_ctf_field_decl const* const*) ret_list; | |
720 | ||
721 | return ret; | |
722 | } | |
723 | ||
724 | const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field) | |
725 | { | |
7f89ddce MD |
726 | if (!field) |
727 | return NULL; | |
728 | ||
729 | return rem_(g_quark_to_string(((struct declaration_field *) field)->name)); | |
64c2c249 | 730 | } |
2bdfa4cf | 731 | |
b14d90be | 732 | const struct declaration *bt_ctf_get_decl_from_def(const struct definition *def) |
2bdfa4cf JD |
733 | { |
734 | if (def) | |
735 | return def->declaration; | |
736 | ||
737 | return NULL; | |
738 | } | |
b14d90be JD |
739 | |
740 | const struct declaration *bt_ctf_get_decl_from_field_decl( | |
741 | const struct bt_ctf_field_decl *field) | |
742 | { | |
743 | if (field) | |
744 | return ((struct declaration_field *) field)->declaration; | |
745 | ||
746 | return NULL; | |
747 | } |