Commit | Line | Data |
---|---|---|
bc37ae52 | 1 | /* |
bc37ae52 JG |
2 | * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
3 | * | |
4 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
e011d2c1 PP |
25 | #define BT_LOG_TAG "TRACE" |
26 | #include <babeltrace/lib-logging-internal.h> | |
27 | ||
8deee039 | 28 | #include <babeltrace/assert-pre-internal.h> |
10b7a2e4 PP |
29 | #include <babeltrace/trace-ir/trace.h> |
30 | #include <babeltrace/trace-ir/trace-class-internal.h> | |
78cf9df6 | 31 | #include <babeltrace/trace-ir/trace-const.h> |
108b91d0 PP |
32 | #include <babeltrace/trace-ir/trace-internal.h> |
33 | #include <babeltrace/trace-ir/clock-class-internal.h> | |
34 | #include <babeltrace/trace-ir/stream-internal.h> | |
35 | #include <babeltrace/trace-ir/stream-class-internal.h> | |
36 | #include <babeltrace/trace-ir/event-internal.h> | |
37 | #include <babeltrace/trace-ir/event-class.h> | |
38 | #include <babeltrace/trace-ir/event-class-internal.h> | |
bc37ae52 | 39 | #include <babeltrace/ctf-writer/functor-internal.h> |
ac0c6bdd | 40 | #include <babeltrace/ctf-writer/clock-internal.h> |
108b91d0 | 41 | #include <babeltrace/trace-ir/field-wrapper-internal.h> |
939190b3 | 42 | #include <babeltrace/trace-ir/field-classes-internal.h> |
108b91d0 PP |
43 | #include <babeltrace/trace-ir/attributes-internal.h> |
44 | #include <babeltrace/trace-ir/utils-internal.h> | |
45 | #include <babeltrace/trace-ir/resolve-field-path-internal.h> | |
3d9990ac | 46 | #include <babeltrace/compiler-internal.h> |
dac5c838 | 47 | #include <babeltrace/values.h> |
ce141536 | 48 | #include <babeltrace/values-const.h> |
95c09b3a | 49 | #include <babeltrace/values-internal.h> |
8138bfe1 | 50 | #include <babeltrace/object.h> |
c55a9f58 | 51 | #include <babeltrace/types.h> |
3d9990ac | 52 | #include <babeltrace/endian-internal.h> |
8b45963b | 53 | #include <babeltrace/assert-internal.h> |
7b33a0e0 | 54 | #include <babeltrace/compat/glib-internal.h> |
dc3fffef | 55 | #include <inttypes.h> |
544d0515 | 56 | #include <stdint.h> |
4a32fda0 | 57 | #include <string.h> |
0fbb9a9f | 58 | #include <stdlib.h> |
bc37ae52 | 59 | |
839d52a5 | 60 | struct bt_trace_is_static_listener_elem { |
78cf9df6 PP |
61 | bt_trace_is_static_listener_func func; |
62 | bt_trace_listener_removed_func removed; | |
3602afb0 PP |
63 | void *data; |
64 | }; | |
65 | ||
7b33a0e0 PP |
66 | #define BT_ASSERT_PRE_TRACE_HOT(_trace) \ |
67 | BT_ASSERT_PRE_HOT((_trace), "Trace", ": %!+t", (_trace)) | |
18acc6f8 | 68 | |
bc37ae52 | 69 | static |
7b33a0e0 | 70 | void destroy_trace(struct bt_object *obj) |
8deee039 PP |
71 | { |
72 | struct bt_trace *trace = (void *) obj; | |
bc37ae52 | 73 | |
7b33a0e0 | 74 | BT_LIB_LOGD("Destroying trace object: %!+t", trace); |
bc37ae52 | 75 | |
8deee039 PP |
76 | /* |
77 | * Call remove listeners first so that everything else still | |
78 | * exists in the trace. | |
79 | */ | |
80 | if (trace->is_static_listeners) { | |
81 | size_t i; | |
bc37ae52 | 82 | |
8deee039 PP |
83 | for (i = 0; i < trace->is_static_listeners->len; i++) { |
84 | struct bt_trace_is_static_listener_elem elem = | |
85 | g_array_index(trace->is_static_listeners, | |
86 | struct bt_trace_is_static_listener_elem, i); | |
bc37ae52 | 87 | |
8deee039 | 88 | if (elem.removed) { |
9e550e5f | 89 | elem.removed((void *) trace, elem.data); |
8deee039 PP |
90 | } |
91 | } | |
92 | ||
93 | g_array_free(trace->is_static_listeners, TRUE); | |
1248f5ea | 94 | trace->is_static_listeners = NULL; |
bc37ae52 JG |
95 | } |
96 | ||
7b33a0e0 PP |
97 | if (trace->name.str) { |
98 | g_string_free(trace->name.str, TRUE); | |
1248f5ea PP |
99 | trace->name.str = NULL; |
100 | trace->name.value = NULL; | |
95c09b3a PP |
101 | } |
102 | ||
7b33a0e0 PP |
103 | if (trace->streams) { |
104 | BT_LOGD_STR("Destroying streams."); | |
105 | g_ptr_array_free(trace->streams, TRUE); | |
1248f5ea | 106 | trace->streams = NULL; |
bc37ae52 JG |
107 | } |
108 | ||
7b33a0e0 PP |
109 | if (trace->stream_classes_stream_count) { |
110 | g_hash_table_destroy(trace->stream_classes_stream_count); | |
1248f5ea | 111 | trace->stream_classes_stream_count = NULL; |
7b33a0e0 | 112 | } |
8deee039 | 113 | |
10b7a2e4 PP |
114 | BT_LOGD_STR("Putting trace's class."); |
115 | bt_object_put_ref(trace->class); | |
116 | trace->class = NULL; | |
7b33a0e0 | 117 | g_free(trace); |
8deee039 PP |
118 | } |
119 | ||
10b7a2e4 | 120 | struct bt_trace *bt_trace_create(struct bt_trace_class *tc) |
8deee039 PP |
121 | { |
122 | struct bt_trace *trace = NULL; | |
8deee039 | 123 | |
10b7a2e4 | 124 | BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc); |
8deee039 PP |
125 | trace = g_new0(struct bt_trace, 1); |
126 | if (!trace) { | |
127 | BT_LOGE_STR("Failed to allocate one trace."); | |
128 | goto error; | |
129 | } | |
130 | ||
10b7a2e4 | 131 | bt_object_init_shared(&trace->base, destroy_trace); |
7b33a0e0 PP |
132 | trace->streams = g_ptr_array_new_with_free_func( |
133 | (GDestroyNotify) bt_object_try_spec_release); | |
134 | if (!trace->streams) { | |
135 | BT_LOGE_STR("Failed to allocate one GPtrArray."); | |
8deee039 PP |
136 | goto error; |
137 | } | |
138 | ||
7b33a0e0 PP |
139 | trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash, |
140 | g_direct_equal); | |
141 | if (!trace->stream_classes_stream_count) { | |
142 | BT_LOGE_STR("Failed to allocate one GHashTable."); | |
143 | goto error; | |
144 | } | |
145 | ||
146 | trace->name.str = g_string_new(NULL); | |
147 | if (!trace->name.str) { | |
148 | BT_LOGE_STR("Failed to allocate one GString."); | |
149 | goto error; | |
150 | } | |
151 | ||
3602afb0 | 152 | trace->is_static_listeners = g_array_new(FALSE, TRUE, |
839d52a5 | 153 | sizeof(struct bt_trace_is_static_listener_elem)); |
3602afb0 PP |
154 | if (!trace->is_static_listeners) { |
155 | BT_LOGE_STR("Failed to allocate one GArray."); | |
156 | goto error; | |
157 | } | |
158 | ||
10b7a2e4 PP |
159 | trace->class = tc; |
160 | bt_object_get_no_null_check(trace->class); | |
7b33a0e0 PP |
161 | BT_LIB_LOGD("Created trace object: %!+t", trace); |
162 | goto end; | |
bc37ae52 | 163 | |
bc37ae52 | 164 | error: |
8138bfe1 | 165 | BT_OBJECT_PUT_REF_AND_RESET(trace); |
7b33a0e0 PP |
166 | |
167 | end: | |
10b7a2e4 | 168 | return trace; |
bc37ae52 JG |
169 | } |
170 | ||
78cf9df6 | 171 | const char *bt_trace_get_name(const struct bt_trace *trace) |
e96045d4 | 172 | { |
18acc6f8 | 173 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
7b33a0e0 | 174 | return trace->name.value; |
e96045d4 JG |
175 | } |
176 | ||
78cf9df6 | 177 | int bt_trace_set_name(struct bt_trace *trace, const char *name) |
e96045d4 | 178 | { |
7b33a0e0 PP |
179 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
180 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
181 | BT_ASSERT_PRE_TRACE_HOT(trace); | |
182 | g_string_assign(trace->name.str, name); | |
183 | trace->name.value = trace->name.str->str; | |
184 | BT_LIB_LOGV("Set trace's name: %!+t", trace); | |
185 | return 0; | |
e96045d4 JG |
186 | } |
187 | ||
78cf9df6 | 188 | uint64_t bt_trace_get_stream_count(const struct bt_trace *trace) |
bc37ae52 | 189 | { |
7b33a0e0 PP |
190 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
191 | return (uint64_t) trace->streams->len; | |
192 | } | |
95c09b3a | 193 | |
7b33a0e0 PP |
194 | struct bt_stream *bt_trace_borrow_stream_by_index( |
195 | struct bt_trace *trace, uint64_t index) | |
196 | { | |
197 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); | |
198 | BT_ASSERT_PRE_VALID_INDEX(index, trace->streams->len); | |
199 | return g_ptr_array_index(trace->streams, index); | |
200 | } | |
18acc6f8 | 201 | |
78cf9df6 PP |
202 | const struct bt_stream *bt_trace_borrow_stream_by_index_const( |
203 | const struct bt_trace *trace, uint64_t index) | |
9e550e5f | 204 | { |
78cf9df6 | 205 | return bt_trace_borrow_stream_by_index((void *) trace, index); |
9e550e5f PP |
206 | } |
207 | ||
78cf9df6 PP |
208 | struct bt_stream *bt_trace_borrow_stream_by_id(struct bt_trace *trace, |
209 | uint64_t id) | |
7b33a0e0 PP |
210 | { |
211 | struct bt_stream *stream = NULL; | |
212 | uint64_t i; | |
bc37ae52 | 213 | |
7b33a0e0 | 214 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
bc37ae52 | 215 | |
7b33a0e0 PP |
216 | for (i = 0; i < trace->streams->len; i++) { |
217 | struct bt_stream *stream_candidate = | |
218 | g_ptr_array_index(trace->streams, i); | |
cfeb617e | 219 | |
7b33a0e0 PP |
220 | if (stream_candidate->id == id) { |
221 | stream = stream_candidate; | |
222 | goto end; | |
223 | } | |
6474e016 | 224 | } |
95c09b3a | 225 | |
bc37ae52 | 226 | end: |
7b33a0e0 | 227 | return stream; |
bc37ae52 JG |
228 | } |
229 | ||
78cf9df6 PP |
230 | const struct bt_stream *bt_trace_borrow_stream_by_id_const( |
231 | const struct bt_trace *trace, uint64_t id) | |
9e550e5f | 232 | { |
78cf9df6 | 233 | return bt_trace_borrow_stream_by_id((void *) trace, id); |
9e550e5f PP |
234 | } |
235 | ||
78cf9df6 | 236 | bt_bool bt_trace_is_static(const struct bt_trace *trace) |
5acf2ae6 | 237 | { |
44ea72c5 | 238 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
7b33a0e0 | 239 | return (bt_bool) trace->is_static; |
5acf2ae6 PP |
240 | } |
241 | ||
78cf9df6 PP |
242 | int bt_trace_make_static(struct bt_trace *trace) |
243 | { uint64_t i; | |
bae99cf9 | 244 | |
7b33a0e0 PP |
245 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
246 | trace->is_static = true; | |
18acc6f8 | 247 | bt_trace_freeze(trace); |
7b33a0e0 | 248 | BT_LIB_LOGV("Trace is now static: %!+t", trace); |
5acf2ae6 | 249 | |
3602afb0 PP |
250 | /* Call all the "trace is static" listeners */ |
251 | for (i = 0; i < trace->is_static_listeners->len; i++) { | |
839d52a5 | 252 | struct bt_trace_is_static_listener_elem elem = |
3602afb0 | 253 | g_array_index(trace->is_static_listeners, |
839d52a5 | 254 | struct bt_trace_is_static_listener_elem, i); |
3602afb0 PP |
255 | |
256 | if (elem.func) { | |
9e550e5f | 257 | elem.func((void *) trace, elem.data); |
3602afb0 PP |
258 | } |
259 | } | |
260 | ||
7b33a0e0 | 261 | return 0; |
3602afb0 PP |
262 | } |
263 | ||
78cf9df6 PP |
264 | int bt_trace_add_is_static_listener( |
265 | const struct bt_trace *c_trace, | |
266 | bt_trace_is_static_listener_func listener, | |
267 | bt_trace_listener_removed_func listener_removed, void *data, | |
7b33a0e0 | 268 | uint64_t *listener_id) |
3602afb0 | 269 | { |
78cf9df6 | 270 | struct bt_trace *trace = (void *) c_trace; |
7b33a0e0 | 271 | uint64_t i; |
839d52a5 | 272 | struct bt_trace_is_static_listener_elem new_elem = { |
3602afb0 | 273 | .func = listener, |
9962ad4b | 274 | .removed = listener_removed, |
3602afb0 PP |
275 | .data = data, |
276 | }; | |
277 | ||
7b33a0e0 PP |
278 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
279 | BT_ASSERT_PRE_NON_NULL(listener, "Listener"); | |
280 | BT_ASSERT_PRE(!trace->is_static, | |
281 | "Trace is already static: %!+t", trace); | |
282 | BT_ASSERT_PRE(trace->in_remove_listener, | |
283 | "Cannot call this function while executing a " | |
284 | "remove listener: %!+t", trace); | |
9962ad4b | 285 | |
3602afb0 PP |
286 | /* Find the next available spot */ |
287 | for (i = 0; i < trace->is_static_listeners->len; i++) { | |
839d52a5 | 288 | struct bt_trace_is_static_listener_elem elem = |
3602afb0 | 289 | g_array_index(trace->is_static_listeners, |
839d52a5 | 290 | struct bt_trace_is_static_listener_elem, i); |
3602afb0 PP |
291 | |
292 | if (!elem.func) { | |
293 | break; | |
294 | } | |
295 | } | |
296 | ||
297 | if (i == trace->is_static_listeners->len) { | |
298 | g_array_append_val(trace->is_static_listeners, new_elem); | |
299 | } else { | |
300 | g_array_insert_val(trace->is_static_listeners, i, new_elem); | |
301 | } | |
302 | ||
7b33a0e0 PP |
303 | if (listener_id) { |
304 | *listener_id = i; | |
305 | } | |
3602afb0 | 306 | |
7b33a0e0 PP |
307 | BT_LIB_LOGV("Added \"trace is static\" listener: " |
308 | "%![trace-]+t, listener-id=%" PRIu64, trace, i); | |
309 | return 0; | |
310 | } | |
311 | ||
312 | BT_ASSERT_PRE_FUNC | |
313 | static | |
78cf9df6 | 314 | bool has_listener_id(const struct bt_trace *trace, uint64_t listener_id) |
7b33a0e0 PP |
315 | { |
316 | BT_ASSERT(listener_id < trace->is_static_listeners->len); | |
317 | return (&g_array_index(trace->is_static_listeners, | |
318 | struct bt_trace_is_static_listener_elem, | |
319 | listener_id))->func != NULL; | |
3602afb0 PP |
320 | } |
321 | ||
78cf9df6 PP |
322 | int bt_trace_remove_is_static_listener(const struct bt_trace *c_trace, |
323 | uint64_t listener_id) | |
3602afb0 | 324 | { |
78cf9df6 | 325 | struct bt_trace *trace = (void *) c_trace; |
839d52a5 | 326 | struct bt_trace_is_static_listener_elem *elem; |
3602afb0 | 327 | |
7b33a0e0 PP |
328 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
329 | BT_ASSERT_PRE(!trace->is_static, | |
330 | "Trace is already static: %!+t", trace); | |
331 | BT_ASSERT_PRE(trace->in_remove_listener, | |
332 | "Cannot call this function while executing a " | |
333 | "remove listener: %!+t", trace); | |
334 | BT_ASSERT_PRE(has_listener_id(trace, listener_id), | |
335 | "Trace has no such \"trace is static\" listener ID: " | |
336 | "%![trace-]+t, %" PRIu64, trace, listener_id); | |
3602afb0 | 337 | elem = &g_array_index(trace->is_static_listeners, |
839d52a5 | 338 | struct bt_trace_is_static_listener_elem, |
3602afb0 | 339 | listener_id); |
7b33a0e0 | 340 | BT_ASSERT(elem->func); |
3602afb0 | 341 | |
9962ad4b PP |
342 | if (elem->removed) { |
343 | /* Call remove listener */ | |
7b33a0e0 PP |
344 | BT_LIB_LOGV("Calling remove listener: " |
345 | "%![trace-]+t, listener-id=%" PRIu64, | |
346 | trace, listener_id); | |
347 | trace->in_remove_listener = true; | |
9e550e5f | 348 | elem->removed((void *) trace, elem->data); |
7b33a0e0 | 349 | trace->in_remove_listener = false; |
9962ad4b PP |
350 | } |
351 | ||
3602afb0 | 352 | elem->func = NULL; |
9962ad4b | 353 | elem->removed = NULL; |
3602afb0 | 354 | elem->data = NULL; |
7b33a0e0 PP |
355 | BT_LIB_LOGV("Removed \"trace is static\" listener: " |
356 | "%![trace-]+t, listener-id=%" PRIu64, | |
357 | trace, listener_id); | |
358 | return 0; | |
359 | } | |
3602afb0 | 360 | |
7b33a0e0 | 361 | BT_HIDDEN |
78cf9df6 | 362 | void _bt_trace_freeze(const struct bt_trace *trace) |
7b33a0e0 | 363 | { |
66fd07a5 | 364 | /* The packet header field class is already frozen */ |
7b33a0e0 | 365 | BT_ASSERT(trace); |
10b7a2e4 PP |
366 | BT_LIB_LOGD("Freezing trace's class: %!+T", trace->class); |
367 | bt_trace_class_freeze(trace->class); | |
7b33a0e0 | 368 | BT_LIB_LOGD("Freezing trace: %!+t", trace); |
78cf9df6 | 369 | ((struct bt_trace *) trace)->frozen = true; |
5acf2ae6 | 370 | } |
a6918753 | 371 | |
7b33a0e0 PP |
372 | BT_HIDDEN |
373 | void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream) | |
374 | { | |
375 | guint count = 0; | |
376 | ||
377 | bt_object_set_parent(&stream->base, &trace->base); | |
378 | g_ptr_array_add(trace->streams, stream); | |
18acc6f8 | 379 | bt_trace_freeze(trace); |
a6918753 | 380 | |
7b33a0e0 PP |
381 | if (bt_g_hash_table_contains(trace->stream_classes_stream_count, |
382 | stream->class)) { | |
383 | count = GPOINTER_TO_UINT(g_hash_table_lookup( | |
384 | trace->stream_classes_stream_count, stream->class)); | |
a6918753 PP |
385 | } |
386 | ||
7b33a0e0 PP |
387 | g_hash_table_insert(trace->stream_classes_stream_count, |
388 | stream->class, GUINT_TO_POINTER(count + 1)); | |
389 | } | |
390 | ||
391 | BT_HIDDEN | |
78cf9df6 PP |
392 | uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace *trace, |
393 | const struct bt_stream_class *stream_class) | |
7b33a0e0 PP |
394 | { |
395 | gpointer orig_key; | |
396 | gpointer value; | |
397 | uint64_t id = 0; | |
398 | ||
399 | BT_ASSERT(stream_class); | |
400 | BT_ASSERT(trace); | |
401 | if (g_hash_table_lookup_extended(trace->stream_classes_stream_count, | |
402 | stream_class, &orig_key, &value)) { | |
403 | id = (uint64_t) GPOINTER_TO_UINT(value); | |
404 | } | |
405 | ||
406 | return id; | |
a6918753 | 407 | } |
10b7a2e4 PP |
408 | |
409 | struct bt_trace_class *bt_trace_borrow_class(struct bt_trace *trace) | |
410 | { | |
411 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); | |
412 | return trace->class; | |
413 | } | |
414 | ||
415 | const struct bt_trace_class *bt_trace_borrow_class_const( | |
416 | const struct bt_trace *trace) | |
417 | { | |
418 | return bt_trace_borrow_class((void *) trace); | |
419 | } |