Commit | Line | Data |
---|---|---|
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 | ||
350ad6c1 | 24 | #define BT_LOG_TAG "LIB/TRACE" |
c2d9d9cf | 25 | #include "lib/logging.h" |
e011d2c1 | 26 | |
578e048b | 27 | #include "lib/assert-pre.h" |
3fadfbc0 | 28 | #include <babeltrace2/trace-ir/trace.h> |
3fadfbc0 | 29 | #include <babeltrace2/trace-ir/trace-const.h> |
3fadfbc0 | 30 | #include <babeltrace2/trace-ir/event-class.h> |
578e048b MJ |
31 | #include "ctf-writer/functor.h" |
32 | #include "ctf-writer/clock.h" | |
33 | #include "compat/compiler.h" | |
3fadfbc0 MJ |
34 | #include <babeltrace2/value.h> |
35 | #include <babeltrace2/value-const.h> | |
578e048b | 36 | #include "lib/value.h" |
3fadfbc0 | 37 | #include <babeltrace2/types.h> |
578e048b MJ |
38 | #include "compat/endian.h" |
39 | #include "common/assert.h" | |
40 | #include "compat/glib.h" | |
dc3fffef | 41 | #include <inttypes.h> |
544d0515 | 42 | #include <stdint.h> |
4a32fda0 | 43 | #include <string.h> |
0fbb9a9f | 44 | #include <stdlib.h> |
bc37ae52 | 45 | |
578e048b MJ |
46 | #include "attributes.h" |
47 | #include "clock-class.h" | |
48 | #include "event-class.h" | |
49 | #include "event.h" | |
50 | #include "field-class.h" | |
51 | #include "field-wrapper.h" | |
52 | #include "resolve-field-path.h" | |
53 | #include "stream-class.h" | |
54 | #include "stream.h" | |
55 | #include "trace-class.h" | |
56 | #include "trace.h" | |
57 | #include "utils.h" | |
d24d5663 | 58 | #include "lib/func-status.h" |
578e048b | 59 | |
ad5268b5 FD |
60 | struct bt_trace_destruction_listener_elem { |
61 | bt_trace_destruction_listener_func func; | |
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 | 68 | static |
44c440bc | 69 | void 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 | 75 | /* |
ad5268b5 FD |
76 | * Call destruction listener functions so that everything else |
77 | * still exists in the trace. | |
3dca2276 | 78 | */ |
ad5268b5 FD |
79 | if (trace->destruction_listeners) { |
80 | uint64_t i; | |
3f7d4d90 | 81 | BT_LIB_LOGD("Calling trace destruction listener(s): %!+t", trace); |
c47a6bec SM |
82 | |
83 | /* | |
84 | * The trace's reference count is 0 if we're here. Increment | |
85 | * it to avoid a double-destroy (possibly infinitely recursive). | |
86 | * This could happen for example if a destruction listener did | |
87 | * bt_object_get_ref() (or anything that causes | |
88 | * bt_object_get_ref() to be called) on the trace (ref. | |
89 | * count goes from 0 to 1), and then bt_object_put_ref(): the | |
90 | * reference count would go from 1 to 0 again and this function | |
91 | * would be called again. | |
92 | */ | |
93 | trace->base.ref_count++; | |
94 | ||
ad5268b5 FD |
95 | /* Call all the trace destruction listeners */ |
96 | for (i = 0; i < trace->destruction_listeners->len; i++) { | |
97 | struct bt_trace_destruction_listener_elem elem = | |
98 | g_array_index(trace->destruction_listeners, | |
99 | struct bt_trace_destruction_listener_elem, i); | |
100 | ||
101 | if (elem.func) { | |
102 | elem.func(trace, elem.data); | |
3dca2276 | 103 | } |
c47a6bec SM |
104 | |
105 | /* | |
106 | * The destruction listener should not have kept a | |
107 | * reference to the trace. | |
108 | */ | |
109 | BT_ASSERT_PRE(trace->base.ref_count == 1, "Destruction listener kept a reference to the trace being destroyed: %![trace-]+t", trace); | |
3dca2276 | 110 | } |
ad5268b5 FD |
111 | g_array_free(trace->destruction_listeners, TRUE); |
112 | trace->destruction_listeners = NULL; | |
bc37ae52 JG |
113 | } |
114 | ||
44c440bc PP |
115 | if (trace->name.str) { |
116 | g_string_free(trace->name.str, TRUE); | |
238b7404 PP |
117 | trace->name.str = NULL; |
118 | trace->name.value = NULL; | |
95c09b3a PP |
119 | } |
120 | ||
44c440bc PP |
121 | if (trace->streams) { |
122 | BT_LOGD_STR("Destroying streams."); | |
123 | g_ptr_array_free(trace->streams, TRUE); | |
238b7404 | 124 | trace->streams = NULL; |
bc37ae52 JG |
125 | } |
126 | ||
44c440bc PP |
127 | if (trace->stream_classes_stream_count) { |
128 | g_hash_table_destroy(trace->stream_classes_stream_count); | |
238b7404 | 129 | trace->stream_classes_stream_count = NULL; |
44c440bc | 130 | } |
3dca2276 | 131 | |
862ca4ed PP |
132 | BT_LOGD_STR("Putting trace's class."); |
133 | bt_object_put_ref(trace->class); | |
134 | trace->class = NULL; | |
44c440bc | 135 | g_free(trace); |
3dca2276 PP |
136 | } |
137 | ||
862ca4ed | 138 | struct bt_trace *bt_trace_create(struct bt_trace_class *tc) |
3dca2276 PP |
139 | { |
140 | struct bt_trace *trace = NULL; | |
3dca2276 | 141 | |
862ca4ed | 142 | BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc); |
3dca2276 PP |
143 | trace = g_new0(struct bt_trace, 1); |
144 | if (!trace) { | |
870631a2 | 145 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace."); |
3dca2276 PP |
146 | goto error; |
147 | } | |
148 | ||
862ca4ed | 149 | bt_object_init_shared(&trace->base, destroy_trace); |
44c440bc PP |
150 | trace->streams = g_ptr_array_new_with_free_func( |
151 | (GDestroyNotify) bt_object_try_spec_release); | |
152 | if (!trace->streams) { | |
870631a2 | 153 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
3dca2276 PP |
154 | goto error; |
155 | } | |
156 | ||
44c440bc PP |
157 | trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash, |
158 | g_direct_equal); | |
159 | if (!trace->stream_classes_stream_count) { | |
870631a2 | 160 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable."); |
44c440bc PP |
161 | goto error; |
162 | } | |
163 | ||
164 | trace->name.str = g_string_new(NULL); | |
165 | if (!trace->name.str) { | |
870631a2 | 166 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString."); |
44c440bc PP |
167 | goto error; |
168 | } | |
169 | ||
ad5268b5 FD |
170 | trace->destruction_listeners = g_array_new(FALSE, TRUE, |
171 | sizeof(struct bt_trace_destruction_listener_elem)); | |
172 | if (!trace->destruction_listeners) { | |
870631a2 | 173 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray."); |
3602afb0 PP |
174 | goto error; |
175 | } | |
176 | ||
862ca4ed PP |
177 | trace->class = tc; |
178 | bt_object_get_no_null_check(trace->class); | |
44c440bc PP |
179 | BT_LIB_LOGD("Created trace object: %!+t", trace); |
180 | goto end; | |
bc37ae52 | 181 | |
bc37ae52 | 182 | error: |
65300d60 | 183 | BT_OBJECT_PUT_REF_AND_RESET(trace); |
44c440bc PP |
184 | |
185 | end: | |
862ca4ed | 186 | return trace; |
bc37ae52 JG |
187 | } |
188 | ||
40f4ba76 | 189 | const char *bt_trace_get_name(const struct bt_trace *trace) |
e96045d4 | 190 | { |
cb6f1f7d | 191 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
44c440bc | 192 | return trace->name.value; |
e96045d4 JG |
193 | } |
194 | ||
d24d5663 PP |
195 | enum bt_trace_set_name_status bt_trace_set_name(struct bt_trace *trace, |
196 | const char *name) | |
e96045d4 | 197 | { |
44c440bc PP |
198 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
199 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
200 | BT_ASSERT_PRE_TRACE_HOT(trace); | |
201 | g_string_assign(trace->name.str, name); | |
202 | trace->name.value = trace->name.str->str; | |
3f7d4d90 | 203 | BT_LIB_LOGD("Set trace's name: %!+t", trace); |
d24d5663 | 204 | return BT_FUNC_STATUS_OK; |
e96045d4 JG |
205 | } |
206 | ||
40f4ba76 | 207 | uint64_t bt_trace_get_stream_count(const struct bt_trace *trace) |
bc37ae52 | 208 | { |
44c440bc PP |
209 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
210 | return (uint64_t) trace->streams->len; | |
211 | } | |
95c09b3a | 212 | |
44c440bc PP |
213 | struct bt_stream *bt_trace_borrow_stream_by_index( |
214 | struct bt_trace *trace, uint64_t index) | |
215 | { | |
216 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); | |
217 | BT_ASSERT_PRE_VALID_INDEX(index, trace->streams->len); | |
218 | return g_ptr_array_index(trace->streams, index); | |
219 | } | |
cb6f1f7d | 220 | |
40f4ba76 PP |
221 | const struct bt_stream *bt_trace_borrow_stream_by_index_const( |
222 | const struct bt_trace *trace, uint64_t index) | |
e5be10ef | 223 | { |
40f4ba76 | 224 | return bt_trace_borrow_stream_by_index((void *) trace, index); |
e5be10ef PP |
225 | } |
226 | ||
40f4ba76 PP |
227 | struct bt_stream *bt_trace_borrow_stream_by_id(struct bt_trace *trace, |
228 | uint64_t id) | |
44c440bc PP |
229 | { |
230 | struct bt_stream *stream = NULL; | |
231 | uint64_t i; | |
bc37ae52 | 232 | |
44c440bc | 233 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
bc37ae52 | 234 | |
44c440bc PP |
235 | for (i = 0; i < trace->streams->len; i++) { |
236 | struct bt_stream *stream_candidate = | |
237 | g_ptr_array_index(trace->streams, i); | |
cfeb617e | 238 | |
44c440bc PP |
239 | if (stream_candidate->id == id) { |
240 | stream = stream_candidate; | |
241 | goto end; | |
242 | } | |
6474e016 | 243 | } |
95c09b3a | 244 | |
bc37ae52 | 245 | end: |
44c440bc | 246 | return stream; |
bc37ae52 JG |
247 | } |
248 | ||
40f4ba76 PP |
249 | const struct bt_stream *bt_trace_borrow_stream_by_id_const( |
250 | const struct bt_trace *trace, uint64_t id) | |
e5be10ef | 251 | { |
40f4ba76 | 252 | return bt_trace_borrow_stream_by_id((void *) trace, id); |
e5be10ef PP |
253 | } |
254 | ||
d24d5663 | 255 | enum bt_trace_add_listener_status bt_trace_add_destruction_listener( |
40f4ba76 | 256 | const struct bt_trace *c_trace, |
ad5268b5 FD |
257 | bt_trace_destruction_listener_func listener, |
258 | void *data, uint64_t *listener_id) | |
3602afb0 | 259 | { |
40f4ba76 | 260 | struct bt_trace *trace = (void *) c_trace; |
44c440bc | 261 | uint64_t i; |
ad5268b5 | 262 | struct bt_trace_destruction_listener_elem new_elem = { |
3602afb0 PP |
263 | .func = listener, |
264 | .data = data, | |
265 | }; | |
266 | ||
44c440bc PP |
267 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
268 | BT_ASSERT_PRE_NON_NULL(listener, "Listener"); | |
8480c8cc | 269 | |
3602afb0 | 270 | /* Find the next available spot */ |
ad5268b5 FD |
271 | for (i = 0; i < trace->destruction_listeners->len; i++) { |
272 | struct bt_trace_destruction_listener_elem elem = | |
273 | g_array_index(trace->destruction_listeners, | |
274 | struct bt_trace_destruction_listener_elem, i); | |
3602afb0 PP |
275 | |
276 | if (!elem.func) { | |
277 | break; | |
278 | } | |
279 | } | |
280 | ||
ad5268b5 FD |
281 | if (i == trace->destruction_listeners->len) { |
282 | g_array_append_val(trace->destruction_listeners, new_elem); | |
3602afb0 | 283 | } else { |
ad5268b5 | 284 | g_array_insert_val(trace->destruction_listeners, i, new_elem); |
3602afb0 PP |
285 | } |
286 | ||
44c440bc PP |
287 | if (listener_id) { |
288 | *listener_id = i; | |
289 | } | |
3602afb0 | 290 | |
3f7d4d90 | 291 | BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, " |
ad5268b5 | 292 | "listener-id=%" PRIu64, trace, i); |
d24d5663 | 293 | return BT_FUNC_STATUS_OK; |
44c440bc PP |
294 | } |
295 | ||
296 | BT_ASSERT_PRE_FUNC | |
297 | static | |
40f4ba76 | 298 | bool has_listener_id(const struct bt_trace *trace, uint64_t listener_id) |
44c440bc | 299 | { |
ad5268b5 FD |
300 | BT_ASSERT(listener_id < trace->destruction_listeners->len); |
301 | return (&g_array_index(trace->destruction_listeners, | |
302 | struct bt_trace_destruction_listener_elem, | |
44c440bc | 303 | listener_id))->func != NULL; |
3602afb0 PP |
304 | } |
305 | ||
d24d5663 | 306 | enum bt_trace_remove_listener_status bt_trace_remove_destruction_listener( |
23578e82 | 307 | const struct bt_trace *c_trace, uint64_t listener_id) |
3602afb0 | 308 | { |
40f4ba76 | 309 | struct bt_trace *trace = (void *) c_trace; |
ad5268b5 | 310 | struct bt_trace_destruction_listener_elem *elem; |
3602afb0 | 311 | |
44c440bc | 312 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); |
44c440bc | 313 | BT_ASSERT_PRE(has_listener_id(trace, listener_id), |
ad5268b5 | 314 | "Trace has no such trace destruction listener ID: " |
44c440bc | 315 | "%![trace-]+t, %" PRIu64, trace, listener_id); |
ad5268b5 FD |
316 | elem = &g_array_index(trace->destruction_listeners, |
317 | struct bt_trace_destruction_listener_elem, | |
3602afb0 | 318 | listener_id); |
44c440bc | 319 | BT_ASSERT(elem->func); |
3602afb0 PP |
320 | |
321 | elem->func = NULL; | |
322 | elem->data = NULL; | |
3f7d4d90 | 323 | BT_LIB_LOGD("Removed \"trace destruction listener: " |
44c440bc PP |
324 | "%![trace-]+t, listener-id=%" PRIu64, |
325 | trace, listener_id); | |
d24d5663 | 326 | return BT_FUNC_STATUS_OK; |
44c440bc | 327 | } |
3602afb0 | 328 | |
44c440bc | 329 | BT_HIDDEN |
40f4ba76 | 330 | void _bt_trace_freeze(const struct bt_trace *trace) |
44c440bc | 331 | { |
44c440bc | 332 | BT_ASSERT(trace); |
862ca4ed PP |
333 | BT_LIB_LOGD("Freezing trace's class: %!+T", trace->class); |
334 | bt_trace_class_freeze(trace->class); | |
44c440bc | 335 | BT_LIB_LOGD("Freezing trace: %!+t", trace); |
40f4ba76 | 336 | ((struct bt_trace *) trace)->frozen = true; |
5acf2ae6 | 337 | } |
312c056a | 338 | |
44c440bc PP |
339 | BT_HIDDEN |
340 | void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream) | |
341 | { | |
342 | guint count = 0; | |
343 | ||
344 | bt_object_set_parent(&stream->base, &trace->base); | |
345 | g_ptr_array_add(trace->streams, stream); | |
cb6f1f7d | 346 | bt_trace_freeze(trace); |
312c056a | 347 | |
44c440bc PP |
348 | if (bt_g_hash_table_contains(trace->stream_classes_stream_count, |
349 | stream->class)) { | |
350 | count = GPOINTER_TO_UINT(g_hash_table_lookup( | |
351 | trace->stream_classes_stream_count, stream->class)); | |
312c056a PP |
352 | } |
353 | ||
44c440bc PP |
354 | g_hash_table_insert(trace->stream_classes_stream_count, |
355 | stream->class, GUINT_TO_POINTER(count + 1)); | |
356 | } | |
357 | ||
358 | BT_HIDDEN | |
40f4ba76 PP |
359 | uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace *trace, |
360 | const struct bt_stream_class *stream_class) | |
44c440bc PP |
361 | { |
362 | gpointer orig_key; | |
363 | gpointer value; | |
364 | uint64_t id = 0; | |
365 | ||
366 | BT_ASSERT(stream_class); | |
367 | BT_ASSERT(trace); | |
368 | if (g_hash_table_lookup_extended(trace->stream_classes_stream_count, | |
369 | stream_class, &orig_key, &value)) { | |
370 | id = (uint64_t) GPOINTER_TO_UINT(value); | |
371 | } | |
372 | ||
373 | return id; | |
312c056a | 374 | } |
862ca4ed PP |
375 | |
376 | struct bt_trace_class *bt_trace_borrow_class(struct bt_trace *trace) | |
377 | { | |
378 | BT_ASSERT_PRE_NON_NULL(trace, "Trace"); | |
379 | return trace->class; | |
380 | } | |
381 | ||
382 | const struct bt_trace_class *bt_trace_borrow_class_const( | |
383 | const struct bt_trace *trace) | |
384 | { | |
385 | return bt_trace_borrow_class((void *) trace); | |
386 | } | |
c5b9b441 PP |
387 | |
388 | void bt_trace_get_ref(const struct bt_trace *trace) | |
389 | { | |
390 | bt_object_get_ref(trace); | |
391 | } | |
392 | ||
393 | void bt_trace_put_ref(const struct bt_trace *trace) | |
394 | { | |
395 | bt_object_put_ref(trace); | |
396 | } |