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