lib: update copyrights
[babeltrace.git] / lib / trace-ir / trace.c
CommitLineData
bc37ae52 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
bc37ae52
JG
3 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
bc37ae52
JG
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
e011d2c1
PP
24#define BT_LOG_TAG "TRACE"
25#include <babeltrace/lib-logging-internal.h>
26
3dca2276 27#include <babeltrace/assert-pre-internal.h>
862ca4ed
PP
28#include <babeltrace/trace-ir/trace.h>
29#include <babeltrace/trace-ir/trace-class-internal.h>
40f4ba76 30#include <babeltrace/trace-ir/trace-const.h>
56e18c4c
PP
31#include <babeltrace/trace-ir/trace-internal.h>
32#include <babeltrace/trace-ir/clock-class-internal.h>
33#include <babeltrace/trace-ir/stream-internal.h>
34#include <babeltrace/trace-ir/stream-class-internal.h>
35#include <babeltrace/trace-ir/event-internal.h>
36#include <babeltrace/trace-ir/event-class.h>
37#include <babeltrace/trace-ir/event-class-internal.h>
bc37ae52 38#include <babeltrace/ctf-writer/functor-internal.h>
ac0c6bdd 39#include <babeltrace/ctf-writer/clock-internal.h>
56e18c4c 40#include <babeltrace/trace-ir/field-wrapper-internal.h>
c6bd8523 41#include <babeltrace/trace-ir/field-class-internal.h>
56e18c4c
PP
42#include <babeltrace/trace-ir/attributes-internal.h>
43#include <babeltrace/trace-ir/utils-internal.h>
44#include <babeltrace/trace-ir/resolve-field-path-internal.h>
3d9990ac 45#include <babeltrace/compiler-internal.h>
c6bd8523
PP
46#include <babeltrace/value.h>
47#include <babeltrace/value-const.h>
48#include <babeltrace/value-internal.h>
65300d60 49#include <babeltrace/object.h>
c55a9f58 50#include <babeltrace/types.h>
3d9990ac 51#include <babeltrace/endian-internal.h>
f6ccaed9 52#include <babeltrace/assert-internal.h>
44c440bc 53#include <babeltrace/compat/glib-internal.h>
dc3fffef 54#include <inttypes.h>
544d0515 55#include <stdint.h>
4a32fda0 56#include <string.h>
0fbb9a9f 57#include <stdlib.h>
bc37ae52 58
50842bdc 59struct bt_trace_is_static_listener_elem {
40f4ba76
PP
60 bt_trace_is_static_listener_func func;
61 bt_trace_listener_removed_func removed;
3602afb0
PP
62 void *data;
63};
64
44c440bc
PP
65#define BT_ASSERT_PRE_TRACE_HOT(_trace) \
66 BT_ASSERT_PRE_HOT((_trace), "Trace", ": %!+t", (_trace))
cb6f1f7d 67
bc37ae52 68static
44c440bc 69void destroy_trace(struct bt_object *obj)
3dca2276
PP
70{
71 struct bt_trace *trace = (void *) obj;
bc37ae52 72
44c440bc 73 BT_LIB_LOGD("Destroying trace object: %!+t", trace);
bc37ae52 74
3dca2276
PP
75 /*
76 * Call remove listeners first so that everything else still
77 * exists in the trace.
78 */
79 if (trace->is_static_listeners) {
80 size_t i;
bc37ae52 81
3dca2276
PP
82 for (i = 0; i < trace->is_static_listeners->len; i++) {
83 struct bt_trace_is_static_listener_elem elem =
84 g_array_index(trace->is_static_listeners,
85 struct bt_trace_is_static_listener_elem, i);
bc37ae52 86
3dca2276 87 if (elem.removed) {
e5be10ef 88 elem.removed((void *) trace, elem.data);
3dca2276
PP
89 }
90 }
91
92 g_array_free(trace->is_static_listeners, TRUE);
238b7404 93 trace->is_static_listeners = NULL;
bc37ae52
JG
94 }
95
44c440bc
PP
96 if (trace->name.str) {
97 g_string_free(trace->name.str, TRUE);
238b7404
PP
98 trace->name.str = NULL;
99 trace->name.value = NULL;
95c09b3a
PP
100 }
101
44c440bc
PP
102 if (trace->streams) {
103 BT_LOGD_STR("Destroying streams.");
104 g_ptr_array_free(trace->streams, TRUE);
238b7404 105 trace->streams = NULL;
bc37ae52
JG
106 }
107
44c440bc
PP
108 if (trace->stream_classes_stream_count) {
109 g_hash_table_destroy(trace->stream_classes_stream_count);
238b7404 110 trace->stream_classes_stream_count = NULL;
44c440bc 111 }
3dca2276 112
862ca4ed
PP
113 BT_LOGD_STR("Putting trace's class.");
114 bt_object_put_ref(trace->class);
115 trace->class = NULL;
44c440bc 116 g_free(trace);
3dca2276
PP
117}
118
862ca4ed 119struct bt_trace *bt_trace_create(struct bt_trace_class *tc)
3dca2276
PP
120{
121 struct bt_trace *trace = NULL;
3dca2276 122
862ca4ed 123 BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc);
3dca2276
PP
124 trace = g_new0(struct bt_trace, 1);
125 if (!trace) {
126 BT_LOGE_STR("Failed to allocate one trace.");
127 goto error;
128 }
129
862ca4ed 130 bt_object_init_shared(&trace->base, destroy_trace);
44c440bc
PP
131 trace->streams = g_ptr_array_new_with_free_func(
132 (GDestroyNotify) bt_object_try_spec_release);
133 if (!trace->streams) {
134 BT_LOGE_STR("Failed to allocate one GPtrArray.");
3dca2276
PP
135 goto error;
136 }
137
44c440bc
PP
138 trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash,
139 g_direct_equal);
140 if (!trace->stream_classes_stream_count) {
141 BT_LOGE_STR("Failed to allocate one GHashTable.");
142 goto error;
143 }
144
145 trace->name.str = g_string_new(NULL);
146 if (!trace->name.str) {
147 BT_LOGE_STR("Failed to allocate one GString.");
148 goto error;
149 }
150
3602afb0 151 trace->is_static_listeners = g_array_new(FALSE, TRUE,
50842bdc 152 sizeof(struct bt_trace_is_static_listener_elem));
3602afb0
PP
153 if (!trace->is_static_listeners) {
154 BT_LOGE_STR("Failed to allocate one GArray.");
155 goto error;
156 }
157
862ca4ed
PP
158 trace->class = tc;
159 bt_object_get_no_null_check(trace->class);
44c440bc
PP
160 BT_LIB_LOGD("Created trace object: %!+t", trace);
161 goto end;
bc37ae52 162
bc37ae52 163error:
65300d60 164 BT_OBJECT_PUT_REF_AND_RESET(trace);
44c440bc
PP
165
166end:
862ca4ed 167 return trace;
bc37ae52
JG
168}
169
40f4ba76 170const char *bt_trace_get_name(const struct bt_trace *trace)
e96045d4 171{
cb6f1f7d 172 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 173 return trace->name.value;
e96045d4
JG
174}
175
40f4ba76 176int bt_trace_set_name(struct bt_trace *trace, const char *name)
e96045d4 177{
44c440bc
PP
178 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
179 BT_ASSERT_PRE_NON_NULL(name, "Name");
180 BT_ASSERT_PRE_TRACE_HOT(trace);
181 g_string_assign(trace->name.str, name);
182 trace->name.value = trace->name.str->str;
183 BT_LIB_LOGV("Set trace's name: %!+t", trace);
184 return 0;
e96045d4
JG
185}
186
40f4ba76 187uint64_t bt_trace_get_stream_count(const struct bt_trace *trace)
bc37ae52 188{
44c440bc
PP
189 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
190 return (uint64_t) trace->streams->len;
191}
95c09b3a 192
44c440bc
PP
193struct bt_stream *bt_trace_borrow_stream_by_index(
194 struct bt_trace *trace, uint64_t index)
195{
196 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
197 BT_ASSERT_PRE_VALID_INDEX(index, trace->streams->len);
198 return g_ptr_array_index(trace->streams, index);
199}
cb6f1f7d 200
40f4ba76
PP
201const struct bt_stream *bt_trace_borrow_stream_by_index_const(
202 const struct bt_trace *trace, uint64_t index)
e5be10ef 203{
40f4ba76 204 return bt_trace_borrow_stream_by_index((void *) trace, index);
e5be10ef
PP
205}
206
40f4ba76
PP
207struct bt_stream *bt_trace_borrow_stream_by_id(struct bt_trace *trace,
208 uint64_t id)
44c440bc
PP
209{
210 struct bt_stream *stream = NULL;
211 uint64_t i;
bc37ae52 212
44c440bc 213 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
bc37ae52 214
44c440bc
PP
215 for (i = 0; i < trace->streams->len; i++) {
216 struct bt_stream *stream_candidate =
217 g_ptr_array_index(trace->streams, i);
cfeb617e 218
44c440bc
PP
219 if (stream_candidate->id == id) {
220 stream = stream_candidate;
221 goto end;
222 }
6474e016 223 }
95c09b3a 224
bc37ae52 225end:
44c440bc 226 return stream;
bc37ae52
JG
227}
228
40f4ba76
PP
229const struct bt_stream *bt_trace_borrow_stream_by_id_const(
230 const struct bt_trace *trace, uint64_t id)
e5be10ef 231{
40f4ba76 232 return bt_trace_borrow_stream_by_id((void *) trace, id);
e5be10ef
PP
233}
234
40f4ba76 235bt_bool bt_trace_is_static(const struct bt_trace *trace)
5acf2ae6 236{
d975f66c 237 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 238 return (bt_bool) trace->is_static;
5acf2ae6
PP
239}
240
40f4ba76
PP
241int bt_trace_make_static(struct bt_trace *trace)
242{ uint64_t i;
726106d3 243
44c440bc
PP
244 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
245 trace->is_static = true;
cb6f1f7d 246 bt_trace_freeze(trace);
44c440bc 247 BT_LIB_LOGV("Trace is now static: %!+t", trace);
5acf2ae6 248
3602afb0
PP
249 /* Call all the "trace is static" listeners */
250 for (i = 0; i < trace->is_static_listeners->len; i++) {
50842bdc 251 struct bt_trace_is_static_listener_elem elem =
3602afb0 252 g_array_index(trace->is_static_listeners,
50842bdc 253 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
254
255 if (elem.func) {
e5be10ef 256 elem.func((void *) trace, elem.data);
3602afb0
PP
257 }
258 }
259
44c440bc 260 return 0;
3602afb0
PP
261}
262
40f4ba76
PP
263int bt_trace_add_is_static_listener(
264 const struct bt_trace *c_trace,
265 bt_trace_is_static_listener_func listener,
266 bt_trace_listener_removed_func listener_removed, void *data,
44c440bc 267 uint64_t *listener_id)
3602afb0 268{
40f4ba76 269 struct bt_trace *trace = (void *) c_trace;
44c440bc 270 uint64_t i;
50842bdc 271 struct bt_trace_is_static_listener_elem new_elem = {
3602afb0 272 .func = listener,
8480c8cc 273 .removed = listener_removed,
3602afb0
PP
274 .data = data,
275 };
276
44c440bc
PP
277 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
278 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
279 BT_ASSERT_PRE(!trace->is_static,
280 "Trace is already static: %!+t", trace);
281 BT_ASSERT_PRE(trace->in_remove_listener,
282 "Cannot call this function while executing a "
283 "remove listener: %!+t", trace);
8480c8cc 284
3602afb0
PP
285 /* Find the next available spot */
286 for (i = 0; i < trace->is_static_listeners->len; i++) {
50842bdc 287 struct bt_trace_is_static_listener_elem elem =
3602afb0 288 g_array_index(trace->is_static_listeners,
50842bdc 289 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
290
291 if (!elem.func) {
292 break;
293 }
294 }
295
296 if (i == trace->is_static_listeners->len) {
297 g_array_append_val(trace->is_static_listeners, new_elem);
298 } else {
299 g_array_insert_val(trace->is_static_listeners, i, new_elem);
300 }
301
44c440bc
PP
302 if (listener_id) {
303 *listener_id = i;
304 }
3602afb0 305
44c440bc
PP
306 BT_LIB_LOGV("Added \"trace is static\" listener: "
307 "%![trace-]+t, listener-id=%" PRIu64, trace, i);
308 return 0;
309}
310
311BT_ASSERT_PRE_FUNC
312static
40f4ba76 313bool has_listener_id(const struct bt_trace *trace, uint64_t listener_id)
44c440bc
PP
314{
315 BT_ASSERT(listener_id < trace->is_static_listeners->len);
316 return (&g_array_index(trace->is_static_listeners,
317 struct bt_trace_is_static_listener_elem,
318 listener_id))->func != NULL;
3602afb0
PP
319}
320
40f4ba76
PP
321int bt_trace_remove_is_static_listener(const struct bt_trace *c_trace,
322 uint64_t listener_id)
3602afb0 323{
40f4ba76 324 struct bt_trace *trace = (void *) c_trace;
50842bdc 325 struct bt_trace_is_static_listener_elem *elem;
3602afb0 326
44c440bc
PP
327 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
328 BT_ASSERT_PRE(!trace->is_static,
329 "Trace is already static: %!+t", trace);
330 BT_ASSERT_PRE(trace->in_remove_listener,
331 "Cannot call this function while executing a "
332 "remove listener: %!+t", trace);
333 BT_ASSERT_PRE(has_listener_id(trace, listener_id),
334 "Trace has no such \"trace is static\" listener ID: "
335 "%![trace-]+t, %" PRIu64, trace, listener_id);
3602afb0 336 elem = &g_array_index(trace->is_static_listeners,
50842bdc 337 struct bt_trace_is_static_listener_elem,
3602afb0 338 listener_id);
44c440bc 339 BT_ASSERT(elem->func);
3602afb0 340
8480c8cc
PP
341 if (elem->removed) {
342 /* Call remove listener */
44c440bc
PP
343 BT_LIB_LOGV("Calling remove listener: "
344 "%![trace-]+t, listener-id=%" PRIu64,
345 trace, listener_id);
346 trace->in_remove_listener = true;
e5be10ef 347 elem->removed((void *) trace, elem->data);
44c440bc 348 trace->in_remove_listener = false;
8480c8cc
PP
349 }
350
3602afb0 351 elem->func = NULL;
8480c8cc 352 elem->removed = NULL;
3602afb0 353 elem->data = NULL;
44c440bc
PP
354 BT_LIB_LOGV("Removed \"trace is static\" listener: "
355 "%![trace-]+t, listener-id=%" PRIu64,
356 trace, listener_id);
357 return 0;
358}
3602afb0 359
44c440bc 360BT_HIDDEN
40f4ba76 361void _bt_trace_freeze(const struct bt_trace *trace)
44c440bc 362{
e6276565 363 /* The packet header field class is already frozen */
44c440bc 364 BT_ASSERT(trace);
862ca4ed
PP
365 BT_LIB_LOGD("Freezing trace's class: %!+T", trace->class);
366 bt_trace_class_freeze(trace->class);
44c440bc 367 BT_LIB_LOGD("Freezing trace: %!+t", trace);
40f4ba76 368 ((struct bt_trace *) trace)->frozen = true;
5acf2ae6 369}
312c056a 370
44c440bc
PP
371BT_HIDDEN
372void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream)
373{
374 guint count = 0;
375
376 bt_object_set_parent(&stream->base, &trace->base);
377 g_ptr_array_add(trace->streams, stream);
cb6f1f7d 378 bt_trace_freeze(trace);
312c056a 379
44c440bc
PP
380 if (bt_g_hash_table_contains(trace->stream_classes_stream_count,
381 stream->class)) {
382 count = GPOINTER_TO_UINT(g_hash_table_lookup(
383 trace->stream_classes_stream_count, stream->class));
312c056a
PP
384 }
385
44c440bc
PP
386 g_hash_table_insert(trace->stream_classes_stream_count,
387 stream->class, GUINT_TO_POINTER(count + 1));
388}
389
390BT_HIDDEN
40f4ba76
PP
391uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace *trace,
392 const struct bt_stream_class *stream_class)
44c440bc
PP
393{
394 gpointer orig_key;
395 gpointer value;
396 uint64_t id = 0;
397
398 BT_ASSERT(stream_class);
399 BT_ASSERT(trace);
400 if (g_hash_table_lookup_extended(trace->stream_classes_stream_count,
401 stream_class, &orig_key, &value)) {
402 id = (uint64_t) GPOINTER_TO_UINT(value);
403 }
404
405 return id;
312c056a 406}
862ca4ed
PP
407
408struct bt_trace_class *bt_trace_borrow_class(struct bt_trace *trace)
409{
410 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
411 return trace->class;
412}
413
414const struct bt_trace_class *bt_trace_borrow_class_const(
415 const struct bt_trace *trace)
416{
417 return bt_trace_borrow_class((void *) trace);
418}
This page took 0.07689 seconds and 4 git commands to generate.