794c54224f0ceb3fdac371a590b38bc1e8168b5d
[babeltrace.git] / src / plugins / ctf / common / metadata / ctf-meta-update-value-storing-indexes.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #include <babeltrace2/babeltrace.h>
8 #include "common/macros.h"
9 #include "common/assert.h"
10 #include <glib.h>
11 #include <stdint.h>
12 #include <string.h>
13 #include <inttypes.h>
14
15 #include "ctf-meta-visitors.hpp"
16
17 static
18 int update_field_class_stored_value_index(struct ctf_field_class *fc,
19 struct ctf_trace_class *tc,
20 struct ctf_stream_class *sc,
21 struct ctf_event_class *ec)
22 {
23 int ret = 0;
24 uint64_t i;
25 struct ctf_field_path *field_path = NULL;
26 struct ctf_field_class_int *tgt_fc = NULL;
27 uint64_t *stored_value_index = NULL;
28
29 if (!fc) {
30 goto end;
31 }
32
33 switch (fc->type) {
34 case CTF_FIELD_CLASS_TYPE_VARIANT:
35 {
36 ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc);
37
38 field_path = &var_fc->tag_path;
39 stored_value_index = &var_fc->stored_tag_index;
40 tgt_fc = &var_fc->tag_fc->base;
41 break;
42 }
43 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
44 {
45 struct ctf_field_class_sequence *seq_fc = ctf_field_class_as_sequence(fc);
46
47 field_path = &seq_fc->length_path;
48 stored_value_index = &seq_fc->stored_length_index;
49 tgt_fc = seq_fc->length_fc;
50 break;
51 }
52 default:
53 break;
54 }
55
56 if (field_path) {
57 BT_ASSERT(tgt_fc);
58 BT_ASSERT(tgt_fc->base.base.type == CTF_FIELD_CLASS_TYPE_INT ||
59 tgt_fc->base.base.type == CTF_FIELD_CLASS_TYPE_ENUM);
60 if (tgt_fc->storing_index >= 0) {
61 /* Already storing its value */
62 *stored_value_index = (uint64_t) tgt_fc->storing_index;
63 } else {
64 /* Not storing its value: allocate new index */
65 tgt_fc->storing_index = tc->stored_value_count;
66 *stored_value_index = (uint64_t) tgt_fc->storing_index;
67 tc->stored_value_count++;
68 }
69 }
70
71 switch (fc->type) {
72 case CTF_FIELD_CLASS_TYPE_STRUCT:
73 {
74 struct ctf_field_class_struct *struct_fc = ctf_field_class_as_struct(fc);
75
76 for (i = 0; i < struct_fc->members->len; i++) {
77 struct ctf_named_field_class *named_fc =
78 ctf_field_class_struct_borrow_member_by_index(
79 struct_fc, i);
80
81 ret = update_field_class_stored_value_index(named_fc->fc,
82 tc, sc, ec);
83 if (ret) {
84 goto end;
85 }
86 }
87
88 break;
89 }
90 case CTF_FIELD_CLASS_TYPE_VARIANT:
91 {
92 struct ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc);
93
94 for (i = 0; i < var_fc->options->len; i++) {
95 struct ctf_named_field_class *named_fc =
96 ctf_field_class_variant_borrow_option_by_index(
97 var_fc, i);
98
99 ret = update_field_class_stored_value_index(named_fc->fc,
100 tc, sc, ec);
101 if (ret) {
102 goto end;
103 }
104 }
105
106 break;
107 }
108 case CTF_FIELD_CLASS_TYPE_ARRAY:
109 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
110 {
111 struct ctf_field_class_array_base *array_fc = ctf_field_class_as_array_base(fc);
112
113 ret = update_field_class_stored_value_index(array_fc->elem_fc,
114 tc, sc, ec);
115 if (ret) {
116 goto end;
117 }
118
119 break;
120 }
121 default:
122 break;
123 }
124
125 end:
126 return ret;
127 }
128
129 BT_HIDDEN
130 int ctf_trace_class_update_value_storing_indexes(struct ctf_trace_class *ctf_tc)
131 {
132 uint64_t i;
133
134 if (!ctf_tc->is_translated) {
135 update_field_class_stored_value_index(
136 ctf_tc->packet_header_fc, ctf_tc, NULL, NULL);
137 }
138
139 for (i = 0; i < ctf_tc->stream_classes->len; i++) {
140 uint64_t j;
141 ctf_stream_class *sc = (ctf_stream_class *) ctf_tc->stream_classes->pdata[i];
142
143 if (!sc->is_translated) {
144 update_field_class_stored_value_index(sc->packet_context_fc,
145 ctf_tc, sc, NULL);
146 update_field_class_stored_value_index(sc->event_header_fc,
147 ctf_tc, sc, NULL);
148 update_field_class_stored_value_index(
149 sc->event_common_context_fc, ctf_tc, sc, NULL);
150 }
151
152 for (j = 0; j < sc->event_classes->len; j++) {
153 struct ctf_event_class *ec =
154 (ctf_event_class *) sc->event_classes->pdata[j];
155
156 if (!ec->is_translated) {
157 update_field_class_stored_value_index(
158 ec->spec_context_fc, ctf_tc, sc, ec);
159 update_field_class_stored_value_index(
160 ec->payload_fc, ctf_tc, sc, ec);
161 }
162 }
163 }
164
165 return 0;
166 }
This page took 0.031319 seconds and 3 git commands to generate.