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