Standardize `!ptr` i/o `ptr == NULL`, `ptr` i/o `ptr != NULL`
[babeltrace.git] / src / lib / trace-ir / trace.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
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
24 #define BT_LOG_TAG "LIB/TRACE"
25 #include "lib/logging.h"
26
27 #include "lib/assert-pre.h"
28 #include <babeltrace2/trace-ir/trace.h>
29 #include <babeltrace2/trace-ir/trace-const.h>
30 #include <babeltrace2/trace-ir/event-class.h>
31 #include "ctf-writer/functor.h"
32 #include "ctf-writer/clock.h"
33 #include "compat/compiler.h"
34 #include <babeltrace2/value.h>
35 #include <babeltrace2/value-const.h>
36 #include "lib/value.h"
37 #include <babeltrace2/types.h>
38 #include "compat/endian.h"
39 #include "common/assert.h"
40 #include "compat/glib.h"
41 #include <inttypes.h>
42 #include <stdint.h>
43 #include <string.h>
44 #include <stdlib.h>
45
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 #include "lib/func-status.h"
59
60 struct bt_trace_destruction_listener_elem {
61 bt_trace_destruction_listener_func func;
62 void *data;
63 };
64
65 #define BT_ASSERT_PRE_DEV_TRACE_HOT(_trace) \
66 BT_ASSERT_PRE_DEV_HOT((_trace), "Trace", ": %!+t", (_trace))
67
68 static
69 void destroy_trace(struct bt_object *obj)
70 {
71 struct bt_trace *trace = (void *) obj;
72
73 BT_LIB_LOGD("Destroying trace object: %!+t", trace);
74
75 /*
76 * Call destruction listener functions so that everything else
77 * still exists in the trace.
78 */
79 if (trace->destruction_listeners) {
80 uint64_t i;
81 BT_LIB_LOGD("Calling trace destruction listener(s): %!+t", trace);
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
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);
103 }
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);
110 }
111 g_array_free(trace->destruction_listeners, TRUE);
112 trace->destruction_listeners = NULL;
113 }
114
115 if (trace->name.str) {
116 g_string_free(trace->name.str, TRUE);
117 trace->name.str = NULL;
118 trace->name.value = NULL;
119 }
120
121 if (trace->environment) {
122 BT_LOGD_STR("Destroying environment attributes.");
123 bt_attributes_destroy(trace->environment);
124 trace->environment = NULL;
125 }
126
127 if (trace->streams) {
128 BT_LOGD_STR("Destroying streams.");
129 g_ptr_array_free(trace->streams, TRUE);
130 trace->streams = NULL;
131 }
132
133 if (trace->stream_classes_stream_count) {
134 g_hash_table_destroy(trace->stream_classes_stream_count);
135 trace->stream_classes_stream_count = NULL;
136 }
137
138 BT_LOGD_STR("Putting trace's class.");
139 bt_object_put_ref(trace->class);
140 trace->class = NULL;
141 g_free(trace);
142 }
143
144 struct bt_trace *bt_trace_create(struct bt_trace_class *tc)
145 {
146 struct bt_trace *trace = NULL;
147
148 BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc);
149 trace = g_new0(struct bt_trace, 1);
150 if (!trace) {
151 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace.");
152 goto error;
153 }
154
155 bt_object_init_shared(&trace->base, destroy_trace);
156 trace->streams = g_ptr_array_new_with_free_func(
157 (GDestroyNotify) bt_object_try_spec_release);
158 if (!trace->streams) {
159 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
160 goto error;
161 }
162
163 trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash,
164 g_direct_equal);
165 if (!trace->stream_classes_stream_count) {
166 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable.");
167 goto error;
168 }
169
170 trace->name.str = g_string_new(NULL);
171 if (!trace->name.str) {
172 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
173 goto error;
174 }
175
176 trace->environment = bt_attributes_create();
177 if (!trace->environment) {
178 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object.");
179 goto error;
180 }
181
182 trace->destruction_listeners = g_array_new(FALSE, TRUE,
183 sizeof(struct bt_trace_destruction_listener_elem));
184 if (!trace->destruction_listeners) {
185 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
186 goto error;
187 }
188
189 trace->class = tc;
190 bt_object_get_no_null_check(trace->class);
191 BT_LIB_LOGD("Created trace object: %!+t", trace);
192 goto end;
193
194 error:
195 BT_OBJECT_PUT_REF_AND_RESET(trace);
196
197 end:
198 return trace;
199 }
200
201 const char *bt_trace_get_name(const struct bt_trace *trace)
202 {
203 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
204 return trace->name.value;
205 }
206
207 enum bt_trace_set_name_status bt_trace_set_name(struct bt_trace *trace,
208 const char *name)
209 {
210 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
211 BT_ASSERT_PRE_NON_NULL(name, "Name");
212 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
213 g_string_assign(trace->name.str, name);
214 trace->name.value = trace->name.str->str;
215 BT_LIB_LOGD("Set trace's name: %!+t", trace);
216 return BT_FUNC_STATUS_OK;
217 }
218
219 bt_uuid bt_trace_get_uuid(const struct bt_trace *trace)
220 {
221 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
222 return trace->uuid.value;
223 }
224
225 void bt_trace_set_uuid(struct bt_trace *trace, bt_uuid uuid)
226 {
227 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
228 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
229 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
230 bt_uuid_copy(trace->uuid.uuid, uuid);
231 trace->uuid.value = trace->uuid.uuid;
232 BT_LIB_LOGD("Set trace's UUID: %!+t", trace);
233 }
234
235 BT_ASSERT_FUNC
236 static
237 bool trace_has_environment_entry(const struct bt_trace *trace, const char *name)
238 {
239 BT_ASSERT(trace);
240
241 return bt_attributes_borrow_field_value_by_name(
242 trace->environment, name);
243 }
244
245 static
246 enum bt_trace_set_environment_entry_status set_environment_entry(
247 struct bt_trace *trace,
248 const char *name, struct bt_value *value)
249 {
250 int ret;
251
252 BT_ASSERT(trace);
253 BT_ASSERT(name);
254 BT_ASSERT(value);
255 BT_ASSERT_PRE(!trace->frozen ||
256 !trace_has_environment_entry(trace, name),
257 "Trace is frozen: cannot replace environment entry: "
258 "%![trace-]+t, entry-name=\"%s\"", trace, name);
259 ret = bt_attributes_set_field_value(trace->environment, name,
260 value);
261 if (ret) {
262 ret = BT_FUNC_STATUS_MEMORY_ERROR;
263 BT_LIB_LOGE_APPEND_CAUSE(
264 "Cannot set trace's environment entry: "
265 "%![trace-]+t, entry-name=\"%s\"", trace, name);
266 } else {
267 bt_value_freeze(value);
268 BT_LIB_LOGD("Set trace's environment entry: "
269 "%![trace-]+t, entry-name=\"%s\"", trace, name);
270 }
271
272 return ret;
273 }
274
275 enum bt_trace_set_environment_entry_status
276 bt_trace_set_environment_entry_string(
277 struct bt_trace *trace, const char *name, const char *value)
278 {
279 int ret;
280 struct bt_value *value_obj;
281 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
282 BT_ASSERT_PRE_NON_NULL(name, "Name");
283 BT_ASSERT_PRE_NON_NULL(value, "Value");
284 value_obj = bt_value_string_create_init(value);
285 if (!value_obj) {
286 BT_LIB_LOGE_APPEND_CAUSE(
287 "Cannot create a string value object.");
288 ret = -1;
289 goto end;
290 }
291
292 /* set_environment_entry() logs errors */
293 ret = set_environment_entry(trace, name, value_obj);
294
295 end:
296 bt_object_put_ref(value_obj);
297 return ret;
298 }
299
300 enum bt_trace_set_environment_entry_status
301 bt_trace_set_environment_entry_integer(
302 struct bt_trace *trace, const char *name, int64_t value)
303 {
304 int ret;
305 struct bt_value *value_obj;
306 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
307 BT_ASSERT_PRE_NON_NULL(name, "Name");
308 value_obj = bt_value_integer_signed_create_init(value);
309 if (!value_obj) {
310 BT_LIB_LOGE_APPEND_CAUSE(
311 "Cannot create an integer value object.");
312 ret = BT_FUNC_STATUS_MEMORY_ERROR;
313 goto end;
314 }
315
316 /* set_environment_entry() logs errors */
317 ret = set_environment_entry(trace, name, value_obj);
318
319 end:
320 bt_object_put_ref(value_obj);
321 return ret;
322 }
323
324 uint64_t bt_trace_get_environment_entry_count(const struct bt_trace *trace)
325 {
326 int64_t ret;
327
328 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
329 ret = bt_attributes_get_count(trace->environment);
330 BT_ASSERT(ret >= 0);
331 return (uint64_t) ret;
332 }
333
334 void bt_trace_borrow_environment_entry_by_index_const(
335 const struct bt_trace *trace, uint64_t index,
336 const char **name, const struct bt_value **value)
337 {
338 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
339 BT_ASSERT_PRE_DEV_NON_NULL(name, "Name");
340 BT_ASSERT_PRE_DEV_NON_NULL(value, "Value");
341 BT_ASSERT_PRE_DEV_VALID_INDEX(index,
342 bt_attributes_get_count(trace->environment));
343 *value = bt_attributes_borrow_field_value(trace->environment, index);
344 BT_ASSERT(*value);
345 *name = bt_attributes_get_field_name(trace->environment, index);
346 BT_ASSERT(*name);
347 }
348
349 const struct bt_value *bt_trace_borrow_environment_entry_value_by_name_const(
350 const struct bt_trace *trace, const char *name)
351 {
352 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
353 BT_ASSERT_PRE_DEV_NON_NULL(name, "Name");
354 return bt_attributes_borrow_field_value_by_name(trace->environment,
355 name);
356 }
357
358 uint64_t bt_trace_get_stream_count(const struct bt_trace *trace)
359 {
360 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
361 return (uint64_t) trace->streams->len;
362 }
363
364 struct bt_stream *bt_trace_borrow_stream_by_index(
365 struct bt_trace *trace, uint64_t index)
366 {
367 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
368 BT_ASSERT_PRE_DEV_VALID_INDEX(index, trace->streams->len);
369 return g_ptr_array_index(trace->streams, index);
370 }
371
372 const struct bt_stream *bt_trace_borrow_stream_by_index_const(
373 const struct bt_trace *trace, uint64_t index)
374 {
375 return bt_trace_borrow_stream_by_index((void *) trace, index);
376 }
377
378 struct bt_stream *bt_trace_borrow_stream_by_id(struct bt_trace *trace,
379 uint64_t id)
380 {
381 struct bt_stream *stream = NULL;
382 uint64_t i;
383
384 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
385
386 for (i = 0; i < trace->streams->len; i++) {
387 struct bt_stream *stream_candidate =
388 g_ptr_array_index(trace->streams, i);
389
390 if (stream_candidate->id == id) {
391 stream = stream_candidate;
392 goto end;
393 }
394 }
395
396 end:
397 return stream;
398 }
399
400 const struct bt_stream *bt_trace_borrow_stream_by_id_const(
401 const struct bt_trace *trace, uint64_t id)
402 {
403 return bt_trace_borrow_stream_by_id((void *) trace, id);
404 }
405
406 enum bt_trace_add_listener_status bt_trace_add_destruction_listener(
407 const struct bt_trace *c_trace,
408 bt_trace_destruction_listener_func listener,
409 void *data, uint64_t *listener_id)
410 {
411 struct bt_trace *trace = (void *) c_trace;
412 uint64_t i;
413 struct bt_trace_destruction_listener_elem new_elem = {
414 .func = listener,
415 .data = data,
416 };
417
418 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
419 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
420
421 /* Find the next available spot */
422 for (i = 0; i < trace->destruction_listeners->len; i++) {
423 struct bt_trace_destruction_listener_elem elem =
424 g_array_index(trace->destruction_listeners,
425 struct bt_trace_destruction_listener_elem, i);
426
427 if (!elem.func) {
428 break;
429 }
430 }
431
432 if (i == trace->destruction_listeners->len) {
433 g_array_append_val(trace->destruction_listeners, new_elem);
434 } else {
435 g_array_insert_val(trace->destruction_listeners, i, new_elem);
436 }
437
438 if (listener_id) {
439 *listener_id = i;
440 }
441
442 BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, "
443 "listener-id=%" PRIu64, trace, i);
444 return BT_FUNC_STATUS_OK;
445 }
446
447 static
448 bool has_listener_id(const struct bt_trace *trace, uint64_t listener_id)
449 {
450 BT_ASSERT(listener_id < trace->destruction_listeners->len);
451 return (&g_array_index(trace->destruction_listeners,
452 struct bt_trace_destruction_listener_elem,
453 listener_id))->func;
454 }
455
456 enum bt_trace_remove_listener_status bt_trace_remove_destruction_listener(
457 const struct bt_trace *c_trace, uint64_t listener_id)
458 {
459 struct bt_trace *trace = (void *) c_trace;
460 struct bt_trace_destruction_listener_elem *elem;
461
462 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
463 BT_ASSERT_PRE(has_listener_id(trace, listener_id),
464 "Trace has no such trace destruction listener ID: "
465 "%![trace-]+t, %" PRIu64, trace, listener_id);
466 elem = &g_array_index(trace->destruction_listeners,
467 struct bt_trace_destruction_listener_elem,
468 listener_id);
469 BT_ASSERT(elem->func);
470
471 elem->func = NULL;
472 elem->data = NULL;
473 BT_LIB_LOGD("Removed \"trace destruction listener: "
474 "%![trace-]+t, listener-id=%" PRIu64,
475 trace, listener_id);
476 return BT_FUNC_STATUS_OK;
477 }
478
479 BT_HIDDEN
480 void _bt_trace_freeze(const struct bt_trace *trace)
481 {
482 BT_ASSERT(trace);
483 BT_LIB_LOGD("Freezing trace's class: %!+T", trace->class);
484 bt_trace_class_freeze(trace->class);
485 BT_LIB_LOGD("Freezing trace: %!+t", trace);
486 ((struct bt_trace *) trace)->frozen = true;
487 }
488
489 BT_HIDDEN
490 void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream)
491 {
492 guint count = 0;
493
494 bt_object_set_parent(&stream->base, &trace->base);
495 g_ptr_array_add(trace->streams, stream);
496 bt_trace_freeze(trace);
497
498 if (bt_g_hash_table_contains(trace->stream_classes_stream_count,
499 stream->class)) {
500 count = GPOINTER_TO_UINT(g_hash_table_lookup(
501 trace->stream_classes_stream_count, stream->class));
502 }
503
504 g_hash_table_insert(trace->stream_classes_stream_count,
505 stream->class, GUINT_TO_POINTER(count + 1));
506 }
507
508 BT_HIDDEN
509 uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace *trace,
510 const struct bt_stream_class *stream_class)
511 {
512 gpointer orig_key;
513 gpointer value;
514 uint64_t id = 0;
515
516 BT_ASSERT(stream_class);
517 BT_ASSERT(trace);
518 if (g_hash_table_lookup_extended(trace->stream_classes_stream_count,
519 stream_class, &orig_key, &value)) {
520 id = (uint64_t) GPOINTER_TO_UINT(value);
521 }
522
523 return id;
524 }
525
526 struct bt_trace_class *bt_trace_borrow_class(struct bt_trace *trace)
527 {
528 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
529 return trace->class;
530 }
531
532 const struct bt_trace_class *bt_trace_borrow_class_const(
533 const struct bt_trace *trace)
534 {
535 return bt_trace_borrow_class((void *) trace);
536 }
537
538 void bt_trace_get_ref(const struct bt_trace *trace)
539 {
540 bt_object_get_ref(trace);
541 }
542
543 void bt_trace_put_ref(const struct bt_trace *trace)
544 {
545 bt_object_put_ref(trace);
546 }
This page took 0.041545 seconds and 4 git commands to generate.