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