lib: create_packet_message(): use pool to determine beginning or end msg
[babeltrace.git] / lib / trace-ir / trace-class.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "TRACE"
25 #include <babeltrace/lib-logging-internal.h>
26
27 #include <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/trace-ir/trace-class.h>
29 #include <babeltrace/trace-ir/trace-class-const.h>
30 #include <babeltrace/trace-ir/trace-internal.h>
31 #include <babeltrace/trace-ir/clock-class-internal.h>
32 #include <babeltrace/trace-ir/stream-internal.h>
33 #include <babeltrace/trace-ir/stream-class-internal.h>
34 #include <babeltrace/trace-ir/event-internal.h>
35 #include <babeltrace/trace-ir/event-class.h>
36 #include <babeltrace/trace-ir/event-class-internal.h>
37 #include <babeltrace/ctf-writer/functor-internal.h>
38 #include <babeltrace/ctf-writer/clock-internal.h>
39 #include <babeltrace/trace-ir/field-wrapper-internal.h>
40 #include <babeltrace/trace-ir/field-class-internal.h>
41 #include <babeltrace/trace-ir/attributes-internal.h>
42 #include <babeltrace/trace-ir/utils-internal.h>
43 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
44 #include <babeltrace/compiler-internal.h>
45 #include <babeltrace/value.h>
46 #include <babeltrace/value-const.h>
47 #include <babeltrace/value-internal.h>
48 #include <babeltrace/types.h>
49 #include <babeltrace/endian-internal.h>
50 #include <babeltrace/assert-internal.h>
51 #include <babeltrace/compat/glib-internal.h>
52 #include <inttypes.h>
53 #include <stdint.h>
54 #include <string.h>
55 #include <stdlib.h>
56
57 struct bt_trace_class_destruction_listener_elem {
58 bt_trace_class_destruction_listener_func func;
59 void *data;
60 };
61
62 #define BT_ASSERT_PRE_TRACE_CLASS_HOT(_tc) \
63 BT_ASSERT_PRE_HOT((_tc), "Trace class", ": %!+T", (_tc))
64
65 static
66 void destroy_trace_class(struct bt_object *obj)
67 {
68 struct bt_trace_class *tc = (void *) obj;
69
70 BT_LIB_LOGD("Destroying trace class object: %!+T", tc);
71 /*
72 * Call destruction listener functions so that everything else
73 * still exists in the trace class.
74 */
75 if (tc->destruction_listeners) {
76 uint64_t i;
77 BT_LIB_LOGV("Calling trace class destruction listener(s): %!+T", tc);
78
79 /*
80 * The trace class' reference count is 0 if we're here. Increment
81 * it to avoid a double-destroy (possibly infinitely recursive).
82 * This could happen for example if a destruction listener did
83 * bt_object_get_ref() (or anything that causes
84 * bt_object_get_ref() to be called) on the trace class (ref.
85 * count goes from 0 to 1), and then bt_object_put_ref(): the
86 * reference count would go from 1 to 0 again and this function
87 * would be called again.
88 */
89 tc->base.ref_count++;
90
91 /* Call all the trace class destruction listeners */
92 for (i = 0; i < tc->destruction_listeners->len; i++) {
93 struct bt_trace_class_destruction_listener_elem elem =
94 g_array_index(tc->destruction_listeners,
95 struct bt_trace_class_destruction_listener_elem, i);
96
97 if (elem.func) {
98 elem.func(tc, elem.data);
99 }
100
101 /*
102 * The destruction listener should not have kept a
103 * reference to the trace class.
104 */
105 BT_ASSERT_PRE(tc->base.ref_count == 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc);
106 }
107 g_array_free(tc->destruction_listeners, TRUE);
108 tc->destruction_listeners = NULL;
109 }
110
111 if (tc->environment) {
112 BT_LOGD_STR("Destroying environment attributes.");
113 bt_attributes_destroy(tc->environment);
114 tc->environment = NULL;
115 }
116
117 if (tc->name.str) {
118 g_string_free(tc->name.str, TRUE);
119 tc->name.str = NULL;
120 tc->name.value = NULL;
121 }
122
123 if (tc->stream_classes) {
124 BT_LOGD_STR("Destroying stream classes.");
125 g_ptr_array_free(tc->stream_classes, TRUE);
126 tc->stream_classes = NULL;
127 }
128
129 g_free(tc);
130 }
131
132 struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp)
133 {
134 struct bt_trace_class *tc = NULL;
135
136 BT_ASSERT_PRE_NON_NULL(self_comp, "Self component");
137 BT_LOGD_STR("Creating default trace class object.");
138 tc = g_new0(struct bt_trace_class, 1);
139 if (!tc) {
140 BT_LOGE_STR("Failed to allocate one trace class.");
141 goto error;
142 }
143
144 bt_object_init_shared_with_parent(&tc->base, destroy_trace_class);
145
146 tc->stream_classes = g_ptr_array_new_with_free_func(
147 (GDestroyNotify) bt_object_try_spec_release);
148 if (!tc->stream_classes) {
149 BT_LOGE_STR("Failed to allocate one GPtrArray.");
150 goto error;
151 }
152
153 tc->name.str = g_string_new(NULL);
154 if (!tc->name.str) {
155 BT_LOGE_STR("Failed to allocate one GString.");
156 goto error;
157 }
158
159 tc->environment = bt_attributes_create();
160 if (!tc->environment) {
161 BT_LOGE_STR("Cannot create empty attributes object.");
162 goto error;
163 }
164
165 tc->destruction_listeners = g_array_new(FALSE, TRUE,
166 sizeof(struct bt_trace_class_destruction_listener_elem));
167 if (!tc->destruction_listeners) {
168 BT_LOGE_STR("Failed to allocate one GArray.");
169 goto error;
170 }
171
172 tc->assigns_automatic_stream_class_id = true;
173 BT_LIB_LOGD("Created trace class object: %!+T", tc);
174 goto end;
175
176 error:
177 BT_OBJECT_PUT_REF_AND_RESET(tc);
178
179 end:
180 return tc;
181 }
182
183 const char *bt_trace_class_get_name(const struct bt_trace_class *tc)
184 {
185 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
186 return tc->name.value;
187 }
188
189 enum bt_trace_class_status bt_trace_class_set_name(
190 struct bt_trace_class *tc, const char *name)
191 {
192 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
193 BT_ASSERT_PRE_NON_NULL(name, "Name");
194 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
195 g_string_assign(tc->name.str, name);
196 tc->name.value = tc->name.str->str;
197 BT_LIB_LOGV("Set trace class's name: %!+T", tc);
198 return BT_TRACE_CLASS_STATUS_OK;
199 }
200
201 bt_uuid bt_trace_class_get_uuid(const struct bt_trace_class *tc)
202 {
203 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
204 return tc->uuid.value;
205 }
206
207 void bt_trace_class_set_uuid(struct bt_trace_class *tc, bt_uuid uuid)
208 {
209 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
210 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
211 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
212 memcpy(tc->uuid.uuid, uuid, BABELTRACE_UUID_LEN);
213 tc->uuid.value = tc->uuid.uuid;
214 BT_LIB_LOGV("Set trace class's UUID: %!+T", tc);
215 }
216
217 enum bt_trace_class_status bt_trace_class_add_destruction_listener(
218 const struct bt_trace_class *_tc,
219 bt_trace_class_destruction_listener_func listener,
220 void *data, uint64_t *listener_id)
221 {
222 struct bt_trace_class *tc = (void *) _tc;
223 uint64_t i;
224 struct bt_trace_class_destruction_listener_elem new_elem = {
225 .func = listener,
226 .data = data,
227 };
228
229 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
230 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
231
232 /* Find the next available spot */
233 for (i = 0; i < tc->destruction_listeners->len; i++) {
234 struct bt_trace_class_destruction_listener_elem elem =
235 g_array_index(tc->destruction_listeners,
236 struct bt_trace_class_destruction_listener_elem, i);
237
238 if (!elem.func) {
239 break;
240 }
241 }
242
243 if (i == tc->destruction_listeners->len) {
244 g_array_append_val(tc->destruction_listeners, new_elem);
245 } else {
246 g_array_insert_val(tc->destruction_listeners, i, new_elem);
247 }
248
249 if (listener_id) {
250 *listener_id = i;
251 }
252
253 BT_LIB_LOGV("Added trace class destruction listener: %![tc-]+T, "
254 "listener-id=%" PRIu64, tc, i);
255 return BT_TRACE_CLASS_STATUS_OK;
256 }
257
258 BT_ASSERT_PRE_FUNC
259 static
260 bool has_listener_id(const struct bt_trace_class *tc, uint64_t listener_id)
261 {
262 BT_ASSERT(listener_id < tc->destruction_listeners->len);
263 return (&g_array_index(tc->destruction_listeners,
264 struct bt_trace_class_destruction_listener_elem,
265 listener_id))->func != NULL;
266 }
267
268 enum bt_trace_class_status bt_trace_class_remove_destruction_listener(
269 const struct bt_trace_class *_tc, uint64_t listener_id)
270 {
271 struct bt_trace_class *tc = (void *) _tc;
272 struct bt_trace_class_destruction_listener_elem *elem;
273
274 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
275 BT_ASSERT_PRE(has_listener_id(tc, listener_id),
276 "Trace class has no such trace class destruction listener ID: "
277 "%![tc-]+T, %" PRIu64, tc, listener_id);
278 elem = &g_array_index(tc->destruction_listeners,
279 struct bt_trace_class_destruction_listener_elem,
280 listener_id);
281 BT_ASSERT(elem->func);
282
283 elem->func = NULL;
284 elem->data = NULL;
285 BT_LIB_LOGV("Removed trace class destruction listener: "
286 "%![tc-]+T, listener-id=%" PRIu64,
287 tc, listener_id);
288 return BT_TRACE_CLASS_STATUS_OK;
289 }
290
291 BT_ASSERT_FUNC
292 static
293 bool trace_has_environment_entry(const struct bt_trace_class *tc, const char *name)
294 {
295 BT_ASSERT(tc);
296
297 return bt_attributes_borrow_field_value_by_name(
298 tc->environment, name) != NULL;
299 }
300
301 static
302 enum bt_trace_class_status set_environment_entry(struct bt_trace_class *tc,
303 const char *name, struct bt_value *value)
304 {
305 int ret;
306
307 BT_ASSERT(tc);
308 BT_ASSERT(name);
309 BT_ASSERT(value);
310 BT_ASSERT_PRE(!tc->frozen ||
311 !trace_has_environment_entry(tc, name),
312 "Trace class is frozen: cannot replace environment entry: "
313 "%![tc-]+T, entry-name=\"%s\"", tc, name);
314 ret = bt_attributes_set_field_value(tc->environment, name,
315 value);
316 if (ret) {
317 ret = BT_TRACE_CLASS_STATUS_NOMEM;
318 BT_LIB_LOGE("Cannot set trace class's environment entry: "
319 "%![tc-]+T, entry-name=\"%s\"", tc, name);
320 } else {
321 bt_value_freeze(value);
322 BT_LIB_LOGV("Set trace class's environment entry: "
323 "%![tc-]+T, entry-name=\"%s\"", tc, name);
324 }
325
326 return ret;
327 }
328
329 enum bt_trace_class_status bt_trace_class_set_environment_entry_string(
330 struct bt_trace_class *tc, const char *name, const char *value)
331 {
332 int ret;
333 struct bt_value *value_obj;
334 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
335 BT_ASSERT_PRE_NON_NULL(name, "Name");
336 BT_ASSERT_PRE_NON_NULL(value, "Value");
337 value_obj = bt_value_string_create_init(value);
338 if (!value_obj) {
339 BT_LOGE_STR("Cannot create a string value object.");
340 ret = -1;
341 goto end;
342 }
343
344 /* set_environment_entry() logs errors */
345 ret = set_environment_entry(tc, name, value_obj);
346
347 end:
348 bt_object_put_ref(value_obj);
349 return ret;
350 }
351
352 enum bt_trace_class_status bt_trace_class_set_environment_entry_integer(
353 struct bt_trace_class *tc, const char *name, int64_t value)
354 {
355 int ret;
356 struct bt_value *value_obj;
357 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
358 BT_ASSERT_PRE_NON_NULL(name, "Name");
359 value_obj = bt_value_signed_integer_create_init(value);
360 if (!value_obj) {
361 BT_LOGE_STR("Cannot create an integer value object.");
362 ret = BT_TRACE_CLASS_STATUS_NOMEM;
363 goto end;
364 }
365
366 /* set_environment_entry() logs errors */
367 ret = set_environment_entry(tc, name, value_obj);
368
369 end:
370 bt_object_put_ref(value_obj);
371 return ret;
372 }
373
374 uint64_t bt_trace_class_get_environment_entry_count(const struct bt_trace_class *tc)
375 {
376 int64_t ret;
377
378 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
379 ret = bt_attributes_get_count(tc->environment);
380 BT_ASSERT(ret >= 0);
381 return (uint64_t) ret;
382 }
383
384 void bt_trace_class_borrow_environment_entry_by_index_const(
385 const struct bt_trace_class *tc, uint64_t index,
386 const char **name, const struct bt_value **value)
387 {
388 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
389 BT_ASSERT_PRE_NON_NULL(name, "Name");
390 BT_ASSERT_PRE_NON_NULL(value, "Value");
391 BT_ASSERT_PRE_VALID_INDEX(index,
392 bt_attributes_get_count(tc->environment));
393 *value = bt_attributes_borrow_field_value(tc->environment, index);
394 BT_ASSERT(*value);
395 *name = bt_attributes_get_field_name(tc->environment, index);
396 BT_ASSERT(*name);
397 }
398
399 const struct bt_value *bt_trace_class_borrow_environment_entry_value_by_name_const(
400 const struct bt_trace_class *tc, const char *name)
401 {
402 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
403 BT_ASSERT_PRE_NON_NULL(name, "Name");
404 return bt_attributes_borrow_field_value_by_name(tc->environment,
405 name);
406 }
407
408 uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc)
409 {
410 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
411 return (uint64_t) tc->stream_classes->len;
412 }
413
414 struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index(
415 struct bt_trace_class *tc, uint64_t index)
416 {
417 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
418 BT_ASSERT_PRE_VALID_INDEX(index, tc->stream_classes->len);
419 return g_ptr_array_index(tc->stream_classes, index);
420 }
421
422 const struct bt_stream_class *
423 bt_trace_class_borrow_stream_class_by_index_const(
424 const struct bt_trace_class *tc, uint64_t index)
425 {
426 return bt_trace_class_borrow_stream_class_by_index(
427 (void *) tc, index);
428 }
429
430 struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id(
431 struct bt_trace_class *tc, uint64_t id)
432 {
433 struct bt_stream_class *stream_class = NULL;
434 uint64_t i;
435
436 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
437
438 for (i = 0; i < tc->stream_classes->len; i++) {
439 struct bt_stream_class *stream_class_candidate =
440 g_ptr_array_index(tc->stream_classes, i);
441
442 if (stream_class_candidate->id == id) {
443 stream_class = stream_class_candidate;
444 goto end;
445 }
446 }
447
448 end:
449 return stream_class;
450 }
451
452 const struct bt_stream_class *
453 bt_trace_class_borrow_stream_class_by_id_const(
454 const struct bt_trace_class *tc, uint64_t id)
455 {
456 return bt_trace_class_borrow_stream_class_by_id((void *) tc, id);
457 }
458
459 BT_HIDDEN
460 void _bt_trace_class_freeze(const struct bt_trace_class *tc)
461 {
462 BT_ASSERT(tc);
463 BT_LIB_LOGD("Freezing trace class: %!+T", tc);
464 ((struct bt_trace_class *) tc)->frozen = true;
465 }
466
467 bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc)
468 {
469 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
470 return (bt_bool) tc->assigns_automatic_stream_class_id;
471 }
472
473 void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc,
474 bt_bool value)
475 {
476 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
477 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
478 tc->assigns_automatic_stream_class_id = (bool) value;
479 BT_LIB_LOGV("Set trace class's automatic stream class ID "
480 "assignment property: %!+T", tc);
481 }
482
483 void bt_trace_class_get_ref(const struct bt_trace_class *trace_class)
484 {
485 bt_object_get_ref(trace_class);
486 }
487
488 void bt_trace_class_put_ref(const struct bt_trace_class *trace_class)
489 {
490 bt_object_put_ref(trace_class);
491 }
This page took 0.038538 seconds and 4 git commands to generate.