4 * Babeltrace CTF IR - Utilities
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/objects.h>
35 #define BT_CTF_ATTR_NAME_INDEX 0
36 #define BT_CTF_ATTR_VALUE_INDEX 1
39 const char * const reserved_keywords_str
[] = {"align", "callsite",
40 "const", "char", "clock", "double", "enum", "env", "event",
41 "floating_point", "float", "integer", "int", "long", "short", "signed",
42 "stream", "string", "struct", "trace", "typealias", "typedef",
43 "unsigned", "variant", "void" "_Bool", "_Complex", "_Imaginary"};
45 static GHashTable
*reserved_keywords_set
;
47 static int global_data_refcount
;
49 static __attribute__((constructor
))
53 const size_t reserved_keywords_count
=
54 sizeof(reserved_keywords_str
) / sizeof(char *);
56 global_data_refcount
++;
61 reserved_keywords_set
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
62 for (i
= 0; i
< reserved_keywords_count
; i
++) {
63 gpointer quark
= GINT_TO_POINTER(g_quark_from_string(
64 reserved_keywords_str
[i
]));
66 g_hash_table_insert(reserved_keywords_set
, quark
, quark
);
72 static __attribute__((destructor
))
73 void trace_finalize(void)
75 if (--global_data_refcount
== 0) {
76 g_hash_table_destroy(reserved_keywords_set
);
80 int bt_ctf_validate_identifier(const char *input_string
)
84 char *save_ptr
, *token
;
86 if (!input_string
|| input_string
[0] == '\0') {
91 string
= strdup(input_string
);
97 token
= strtok_r(string
, " ", &save_ptr
);
99 if (g_hash_table_lookup_extended(reserved_keywords_set
,
100 GINT_TO_POINTER(g_quark_from_string(token
)),
106 token
= strtok_r(NULL
, " ", &save_ptr
);
114 struct bt_object
*bt_ctf_attributes_create(void)
117 * Attributes: array object of array objects, each one
118 * containing two entries: a string object (attributes field
119 * name), and an object (attributes field value).
121 * Example (JSON representation):
124 * ["hostname", "eeppdesk"],
125 * ["sysname", "Linux"],
126 * ["tracer_major", 2],
127 * ["tracer_minor", 5]
130 return bt_object_array_create();
134 void bt_ctf_attributes_destroy(struct bt_object
*attr_obj
)
136 bt_object_put(attr_obj
);
140 int bt_ctf_attributes_get_count(struct bt_object
*attr_obj
)
142 return bt_object_array_size(attr_obj
);
146 const char *bt_ctf_attributes_get_field_name(struct bt_object
*attr_obj
,
150 const char *ret
= NULL
;
151 struct bt_object
*attr_field_obj
= NULL
;
152 struct bt_object
*attr_field_name_obj
= NULL
;
154 if (!attr_obj
|| index
< 0) {
158 attr_field_obj
= bt_object_array_get(attr_obj
, index
);
160 if (!attr_field_obj
) {
164 attr_field_name_obj
= bt_object_array_get(attr_field_obj
,
165 BT_CTF_ATTR_NAME_INDEX
);
167 if (!attr_field_name_obj
) {
171 rc
= bt_object_string_get(attr_field_name_obj
, &ret
);
178 BT_OBJECT_PUT(attr_field_name_obj
);
179 BT_OBJECT_PUT(attr_field_obj
);
185 struct bt_object
*bt_ctf_attributes_get_field_value(struct bt_object
*attr_obj
,
188 struct bt_object
*value_obj
= NULL
;
189 struct bt_object
*attr_field_obj
= NULL
;
191 if (!attr_obj
|| index
< 0) {
195 attr_field_obj
= bt_object_array_get(attr_obj
, index
);
197 if (!attr_field_obj
) {
201 value_obj
= bt_object_array_get(attr_field_obj
,
202 BT_CTF_ATTR_VALUE_INDEX
);
205 BT_OBJECT_PUT(attr_field_obj
);
211 struct bt_object
*bt_ctf_attributes_get_field_by_name(
212 struct bt_object
*attr_obj
, const char *name
)
216 struct bt_object
*value_obj
= NULL
;
217 struct bt_object
*attr_field_name_obj
= NULL
;
219 attr_size
= bt_object_array_size(attr_obj
);
225 for (i
= 0; i
< attr_size
; ++i
) {
227 const char *field_name
;
229 value_obj
= bt_object_array_get(attr_obj
, i
);
235 attr_field_name_obj
= bt_object_array_get(value_obj
, 0);
237 if (!attr_field_name_obj
) {
241 ret
= bt_object_string_get(attr_field_name_obj
, &field_name
);
246 if (!strcmp(field_name
, name
)) {
247 BT_OBJECT_PUT(attr_field_name_obj
);
251 BT_OBJECT_PUT(attr_field_name_obj
);
252 BT_OBJECT_PUT(value_obj
);
258 BT_OBJECT_PUT(attr_field_name_obj
);
259 BT_OBJECT_PUT(value_obj
);
265 int bt_ctf_attributes_set_field_value(struct bt_object
*attr_obj
,
266 const char *name
, struct bt_object
*value_obj
)
269 struct bt_object
*attr_field_obj
= NULL
;
271 if (!attr_obj
|| !name
|| !value_obj
) {
276 attr_field_obj
= bt_ctf_attributes_get_field_by_name(attr_obj
, name
);
278 if (attr_field_obj
) {
279 ret
= bt_object_array_set(attr_field_obj
,
280 BT_CTF_ATTR_VALUE_INDEX
, value_obj
);
284 attr_field_obj
= bt_object_array_create();
286 if (!attr_field_obj
) {
291 ret
= bt_object_array_append_string(attr_field_obj
, name
);
292 ret
|= bt_object_array_append(attr_field_obj
, value_obj
);
298 ret
= bt_object_array_append(attr_obj
, attr_field_obj
);
301 BT_OBJECT_PUT(attr_field_obj
);
307 struct bt_object
*bt_ctf_attributes_get_field_value_by_name(
308 struct bt_object
*attr_obj
, const char *name
)
310 struct bt_object
*value_obj
= NULL
;
311 struct bt_object
*attr_field_obj
= NULL
;
313 if (!attr_obj
|| !name
) {
317 attr_field_obj
= bt_ctf_attributes_get_field_by_name(attr_obj
, name
);
319 if (!attr_field_obj
) {
323 value_obj
= bt_object_array_get(attr_field_obj
,
324 BT_CTF_ATTR_VALUE_INDEX
);
327 BT_OBJECT_PUT(attr_field_obj
);
333 int bt_ctf_attributes_freeze(struct bt_object
*attr_obj
)
344 count
= bt_object_array_size(attr_obj
);
352 * We do not freeze the array itself here, since internal
353 * stuff could need to modify/add attributes. Each attribute
354 * is frozen one by one.
356 for (i
= 0; i
< count
; ++i
) {
357 struct bt_object
*obj
= NULL
;
359 obj
= bt_ctf_attributes_get_field_value(attr_obj
, i
);
366 bt_object_freeze(obj
);
This page took 0.042413 seconds and 5 git commands to generate.