2 * Copyright (C) 2016 Collabora Ltd. <guillaume.desmottes@collabora.co.uk>
4 * gstleaks.c: tracing module detecting object leaks
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * @short_description: detect GstObject and GstMiniObject leaks
25 * A tracing module tracking the lifetime of objects by logging those still
26 * alive when program is exiting and raising a warning.
27 * The type of objects tracked can be filtered using the parameters of the
28 * tracer, for example: GST_TRACERS=leaks(filters="GstEvent,GstMessage",print-traces=true)
39 #endif /* G_OS_UNIX */
41 GST_DEBUG_CATEGORY_STATIC (gst_leaks_debug);
42 #define GST_CAT_DEFAULT gst_leaks_debug
45 GST_DEBUG_CATEGORY_INIT (gst_leaks_debug, "leaks", 0, "leaks tracer");
46 #define gst_leaks_tracer_parent_class parent_class
47 G_DEFINE_TYPE_WITH_CODE (GstLeaksTracer, gst_leaks_tracer,
48 GST_TYPE_TRACER, _do_init);
50 static GstTracerRecord *tr_alive;
51 static GstTracerRecord *tr_refings;
53 static GstTracerRecord *tr_added = NULL;
54 static GstTracerRecord *tr_removed = NULL;
55 #endif /* G_OS_UNIX */
56 static GQueue instances = G_QUEUE_INIT;
68 gchar *creation_trace;
74 object_refing_info_free (ObjectRefingInfo * refinfo)
76 g_free (refinfo->trace);
81 object_refing_infos_free (ObjectRefingInfos * infos)
83 g_list_free_full (infos->refing_infos,
84 (GDestroyNotify) object_refing_info_free);
86 g_free (infos->creation_trace);
91 set_print_stack_trace (GstLeaksTracer * self, GstStructure * params)
94 gboolean check_stack_trace =
95 g_getenv ("GST_LEAKS_TRACER_STACK_TRACE") != NULL;
97 if (!check_stack_trace && params)
98 gst_structure_get_boolean (params, "print-traces", &check_stack_trace);
100 if (!check_stack_trace)
104 /* Test if we can retrieve backtrace */
105 trace = gst_debug_get_stack_trace (0);
107 self->log_stack_trace = TRUE;
110 g_warning ("Can't retrieve backtrace on this system");
115 set_filters (GstLeaksTracer * self, const gchar * filters)
118 GStrv tmp = g_strsplit (filters, ",", -1);
120 self->filter = g_array_sized_new (FALSE, FALSE, sizeof (GType),
121 g_strv_length (tmp));
122 for (i = 0; tmp[i]; i++) {
125 type = g_type_from_name (tmp[i]);
127 /* The type may not yet be known by the type system, typically because
128 * the plugin implementing it as not yet be loaded. Save it for now as
129 * it will have another chance to be added to the filter later in
130 * should_handle_object_type() when/if the object type is actually
132 if (!self->unhandled_filter)
133 self->unhandled_filter = g_hash_table_new (NULL, NULL);
135 g_hash_table_add (self->unhandled_filter,
136 GUINT_TO_POINTER (g_quark_from_string (tmp[i])));
137 g_atomic_int_inc (&self->unhandled_filter_count);
141 GST_DEBUG_OBJECT (self, "add filter on %s", tmp[i]);
143 g_array_append_val (self->filter, type);
150 set_params_from_structure (GstLeaksTracer * self, GstStructure * params)
152 const gchar *filters = gst_structure_get_string (params, "filters");
155 set_filters (self, filters);
156 gst_structure_get_boolean (params, "check-refs", &self->check_refs);
160 set_params (GstLeaksTracer * self)
163 GstStructure *params_struct = NULL;
165 g_object_get (self, "params", ¶ms, NULL);
169 tmp = g_strdup_printf ("leaks,%s", params);
170 params_struct = gst_structure_from_string (tmp, NULL);
174 set_params_from_structure (self, params_struct);
176 set_filters (self, params);
181 set_print_stack_trace (self, params_struct);
184 gst_structure_free (params_struct);
188 should_handle_object_type (GstLeaksTracer * self, GType object_type)
193 /* No filtering, handle all types */
196 if (g_atomic_int_get (&self->unhandled_filter_count)) {
197 GST_OBJECT_LOCK (self);
198 if (self->unhandled_filter) {
201 q = g_type_qname (object_type);
202 if (g_hash_table_contains (self->unhandled_filter, GUINT_TO_POINTER (q))) {
203 g_array_append_val (self->filter, object_type);
204 g_hash_table_remove (self->unhandled_filter, GUINT_TO_POINTER (q));
206 if (g_atomic_int_dec_and_test (&self->unhandled_filter_count))
207 g_clear_pointer (&self->unhandled_filter, g_hash_table_unref);
209 GST_OBJECT_UNLOCK (self);
213 GST_OBJECT_UNLOCK (self);
216 len = self->filter->len;
217 for (i = 0; i < len; i++) {
218 GType type = g_array_index (self->filter, GType, i);
220 if (g_type_is_a (object_type, type))
228 /* The object may be destroyed when we log it using the checkpointing system so
229 * we have to save its type name */
233 const gchar *type_name;
237 object_log_new (gpointer obj)
239 ObjectLog *o = g_slice_new (ObjectLog);
243 if (G_IS_OBJECT (obj))
244 o->type_name = G_OBJECT_TYPE_NAME (obj);
246 o->type_name = g_type_name (GST_MINI_OBJECT_TYPE (obj));
252 object_log_free (ObjectLog * obj)
254 g_slice_free (ObjectLog, obj);
256 #endif /* G_OS_UNIX */
259 handle_object_destroyed (GstLeaksTracer * self, gpointer object)
261 GST_OBJECT_LOCK (self);
264 ("object %p destroyed while the leaks tracer was finalizing. Some threads are still running?",
269 g_hash_table_remove (self->objects, object);
272 g_hash_table_add (self->removed, object_log_new (object));
273 #endif /* G_OS_UNIX */
275 GST_OBJECT_UNLOCK (self);
279 object_weak_cb (gpointer data, GObject * object)
281 GstLeaksTracer *self = data;
283 handle_object_destroyed (self, object);
287 mini_object_weak_cb (gpointer data, GstMiniObject * object)
289 GstLeaksTracer *self = data;
291 handle_object_destroyed (self, object);
295 handle_object_created (GstLeaksTracer * self, gpointer object, GType type,
298 ObjectRefingInfos *infos;
301 if (!should_handle_object_type (self, type))
304 infos = g_malloc0 (sizeof (ObjectRefingInfos));
306 g_object_weak_ref ((GObject *) object, object_weak_cb, self);
308 gst_mini_object_weak_ref (GST_MINI_OBJECT_CAST (object),
309 mini_object_weak_cb, self);
311 GST_OBJECT_LOCK (self);
312 if (self->log_stack_trace)
313 infos->creation_trace = gst_debug_get_stack_trace (0);
315 g_hash_table_insert (self->objects, object, infos);
319 g_hash_table_add (self->added, object_log_new (object));
320 #endif /* G_OS_UNIX */
321 GST_OBJECT_UNLOCK (self);
325 mini_object_created_cb (GstTracer * tracer, GstClockTime ts,
326 GstMiniObject * object)
328 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
330 handle_object_created (self, object, GST_MINI_OBJECT_TYPE (object), FALSE);
334 object_created_cb (GstTracer * tracer, GstClockTime ts, GstObject * object)
336 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
337 GType object_type = G_OBJECT_TYPE (object);
339 /* Can't track tracers as they may be disposed after the leak tracer itself */
340 if (g_type_is_a (object_type, GST_TYPE_TRACER))
343 handle_object_created (self, object, object_type, TRUE);
347 handle_object_reffed (GstLeaksTracer * self, gpointer object, gint new_refcount,
348 gboolean reffed, GstClockTime ts)
350 ObjectRefingInfos *infos;
351 ObjectRefingInfo *refinfo;
353 if (!self->check_refs)
356 GST_OBJECT_LOCK (self);
357 infos = g_hash_table_lookup (self->objects, object);
361 refinfo = g_malloc0 (sizeof (ObjectRefingInfo));
363 refinfo->new_refcount = new_refcount;
364 refinfo->reffed = reffed;
365 if (self->log_stack_trace)
366 refinfo->trace = gst_debug_get_stack_trace (0);
368 infos->refing_infos = g_list_prepend (infos->refing_infos, refinfo);
371 GST_OBJECT_UNLOCK (self);
375 object_reffed_cb (GstTracer * tracer, GstClockTime ts, GstObject * object,
378 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
380 handle_object_reffed (self, object, new_refcount, TRUE, ts);
384 object_unreffed_cb (GstTracer * tracer, GstClockTime ts, GstObject * object,
387 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
389 handle_object_reffed (self, object, new_refcount, FALSE, ts);
393 mini_object_reffed_cb (GstTracer * tracer, GstClockTime ts,
394 GstMiniObject * object, gint new_refcount)
396 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
398 handle_object_reffed (self, object, new_refcount, TRUE, ts);
402 mini_object_unreffed_cb (GstTracer * tracer, GstClockTime ts,
403 GstMiniObject * object, gint new_refcount)
405 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
407 handle_object_reffed (self, object, new_refcount, FALSE, ts);
411 gst_leaks_tracer_init (GstLeaksTracer * self)
413 self->objects = g_hash_table_new_full (NULL, NULL, NULL,
414 (GDestroyNotify) object_refing_infos_free);
416 g_queue_push_tail (&instances, self);
420 gst_leaks_tracer_constructed (GObject * object)
422 GstLeaksTracer *self = GST_LEAKS_TRACER (object);
423 GstTracer *tracer = GST_TRACER (object);
427 gst_tracing_register_hook (tracer, "mini-object-created",
428 G_CALLBACK (mini_object_created_cb));
429 gst_tracing_register_hook (tracer, "object-created",
430 G_CALLBACK (object_created_cb));
432 if (self->check_refs) {
433 gst_tracing_register_hook (tracer, "object-reffed",
434 G_CALLBACK (object_reffed_cb));
435 gst_tracing_register_hook (tracer, "mini-object-reffed",
436 G_CALLBACK (mini_object_reffed_cb));
437 gst_tracing_register_hook (tracer, "mini-object-unreffed",
438 G_CALLBACK (mini_object_unreffed_cb));
439 gst_tracing_register_hook (tracer, "object-unreffed",
440 G_CALLBACK (object_unreffed_cb));
443 /* We rely on weak pointers rather than (mini-)object-destroyed hooks so we
444 * are notified of objects being destroyed even during the shuting down of
445 * the tracing system. */
447 ((GObjectClass *) gst_leaks_tracer_parent_class)->constructed (object);
453 const gchar *type_name;
456 ObjectRefingInfos *infos;
459 /* The content of the returned Leak struct is valid until the self->objects
460 * hash table has been modified. */
462 leak_new (gpointer obj, GType type, guint ref_count, ObjectRefingInfos * infos)
464 Leak *leak = g_slice_new (Leak);
467 leak->type_name = g_type_name (type);
468 leak->ref_count = ref_count;
469 leak->desc = gst_info_strdup_printf ("%" GST_PTR_FORMAT, obj);
476 leak_free (Leak * leak)
479 g_slice_free (Leak, leak);
483 sort_leaks (gconstpointer _a, gconstpointer _b)
485 const Leak *a = _a, *b = _b;
487 return g_strcmp0 (a->type_name, b->type_name);
491 create_leaks_list (GstLeaksTracer * self)
497 g_hash_table_iter_init (&iter, self->objects);
498 while (g_hash_table_iter_next (&iter, &obj, &infos)) {
502 if (GST_IS_OBJECT (obj)) {
503 if (GST_OBJECT_FLAG_IS_SET (obj, GST_OBJECT_FLAG_MAY_BE_LEAKED))
506 type = G_OBJECT_TYPE (obj);
507 ref_count = ((GObject *) obj)->ref_count;
509 if (GST_MINI_OBJECT_FLAG_IS_SET (obj, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED))
512 type = GST_MINI_OBJECT_TYPE (obj);
513 ref_count = ((GstMiniObject *) obj)->refcount;
516 l = g_list_prepend (l, leak_new (obj, type, ref_count, infos));
519 /* Sort leaks by type name so they are grouped together making the output
521 l = g_list_sort (l, sort_leaks);
526 /* Return TRUE if at least one leaked object has been logged */
528 log_leaked (GstLeaksTracer * self)
530 GList *ref, *leaks, *l;
532 leaks = create_leaks_list (self);
536 for (l = leaks; l != NULL; l = g_list_next (l)) {
537 Leak *leak = l->data;
539 gst_tracer_record_log (tr_alive, leak->type_name, leak->obj, leak->desc,
541 leak->infos->creation_trace ? leak->infos->creation_trace : "");
543 leak->infos->refing_infos = g_list_reverse (leak->infos->refing_infos);
544 for (ref = leak->infos->refing_infos; ref; ref = ref->next) {
545 ObjectRefingInfo *refinfo = (ObjectRefingInfo *) ref->data;
547 gst_tracer_record_log (tr_refings, refinfo->ts, leak->type_name,
548 leak->obj, refinfo->reffed ? "reffed" : "unreffed",
549 refinfo->new_refcount, refinfo->trace ? refinfo->trace : "");
553 g_list_free_full (leaks, (GDestroyNotify) leak_free);
559 gst_leaks_tracer_finalize (GObject * object)
561 GstLeaksTracer *self = GST_LEAKS_TRACER (object);
568 /* Tracers are destroyed as part of gst_deinit() so now is a good time to
569 * report all the objects which are still alive. */
570 leaks = log_leaked (self);
572 /* Remove weak references */
573 g_hash_table_iter_init (&iter, self->objects);
574 while (g_hash_table_iter_next (&iter, &obj, NULL)) {
575 if (GST_IS_OBJECT (obj))
576 g_object_weak_unref (obj, object_weak_cb, self);
578 gst_mini_object_weak_unref (GST_MINI_OBJECT_CAST (obj),
579 mini_object_weak_cb, self);
582 g_clear_pointer (&self->objects, g_hash_table_unref);
584 g_array_free (self->filter, TRUE);
585 g_clear_pointer (&self->added, g_hash_table_unref);
586 g_clear_pointer (&self->removed, g_hash_table_unref);
587 g_clear_pointer (&self->unhandled_filter, g_hash_table_unref);
589 g_queue_remove (&instances, self);
592 g_warning ("Leaks detected");
594 ((GObjectClass *) gst_leaks_tracer_parent_class)->finalize (object);
597 #define RECORD_FIELD_TYPE_TS \
598 "ts", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
599 "type", G_TYPE_GTYPE, GST_TYPE_CLOCK_TIME, \
600 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
602 #define RECORD_FIELD_TYPE_NAME \
603 "type-name", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
604 "type", G_TYPE_GTYPE, G_TYPE_STRING, \
605 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
607 #define RECORD_FIELD_ADDRESS \
608 "address", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
609 "type", G_TYPE_GTYPE, G_TYPE_POINTER, \
610 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, \
611 GST_TRACER_VALUE_SCOPE_PROCESS, \
613 #define RECORD_FIELD_DESC \
614 "description", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
615 "type", G_TYPE_GTYPE, G_TYPE_STRING, \
616 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
618 #define RECORD_FIELD_REF_COUNT \
619 "ref-count", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
620 "type", G_TYPE_GTYPE, G_TYPE_UINT, \
621 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
623 #define RECORD_FIELD_TRACE \
624 "trace", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
625 "type", G_TYPE_GTYPE, G_TYPE_STRING, \
626 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
631 sig_usr1_handler_foreach (gpointer data, gpointer user_data)
633 GstLeaksTracer *tracer = data;
635 GST_OBJECT_LOCK (tracer);
636 GST_TRACE_OBJECT (tracer, "start listing currently alive objects");
638 GST_TRACE_OBJECT (tracer, "done listing currently alive objects");
639 GST_OBJECT_UNLOCK (tracer);
643 sig_usr1_handler (G_GNUC_UNUSED int signal)
645 g_queue_foreach (&instances, sig_usr1_handler_foreach, NULL);
649 log_checkpoint (GHashTable * hash, GstTracerRecord * record)
654 g_hash_table_iter_init (&iter, hash);
655 while (g_hash_table_iter_next (&iter, &o, NULL)) {
658 gst_tracer_record_log (record, obj->type_name, obj->object);
663 do_checkpoint (GstLeaksTracer * self)
665 GST_TRACE_OBJECT (self, "listing objects created since last checkpoint");
666 log_checkpoint (self->added, tr_added);
667 GST_TRACE_OBJECT (self, "listing objects removed since last checkpoint");
668 log_checkpoint (self->removed, tr_removed);
670 g_hash_table_remove_all (self->added);
671 g_hash_table_remove_all (self->removed);
675 sig_usr2_handler_foreach (gpointer data, gpointer user_data)
677 GstLeaksTracer *tracer = data;
679 GST_OBJECT_LOCK (tracer);
681 if (!tracer->added) {
682 GST_TRACE_OBJECT (tracer, "First checkpoint, start tracking objects");
684 tracer->added = g_hash_table_new_full (NULL, NULL,
685 (GDestroyNotify) object_log_free, NULL);
686 tracer->removed = g_hash_table_new_full (NULL, NULL,
687 (GDestroyNotify) object_log_free, NULL);
689 do_checkpoint (tracer);
692 GST_OBJECT_UNLOCK (tracer);
696 sig_usr2_handler (G_GNUC_UNUSED int signal)
698 g_queue_foreach (&instances, sig_usr2_handler_foreach, NULL);
704 tr_added = gst_tracer_record_new ("object-added.class",
705 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, NULL);
706 GST_OBJECT_FLAG_SET (tr_added, GST_OBJECT_FLAG_MAY_BE_LEAKED);
708 tr_removed = gst_tracer_record_new ("object-removed.class",
709 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, NULL);
710 GST_OBJECT_FLAG_SET (tr_removed, GST_OBJECT_FLAG_MAY_BE_LEAKED);
712 signal (SIGUSR1, sig_usr1_handler);
713 signal (SIGUSR2, sig_usr2_handler);
715 #endif /* G_OS_UNIX */
718 gst_leaks_tracer_class_init (GstLeaksTracerClass * klass)
720 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
722 gobject_class->constructed = gst_leaks_tracer_constructed;
723 gobject_class->finalize = gst_leaks_tracer_finalize;
725 tr_alive = gst_tracer_record_new ("object-alive.class",
726 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, RECORD_FIELD_DESC,
727 RECORD_FIELD_REF_COUNT, RECORD_FIELD_TRACE, NULL);
728 GST_OBJECT_FLAG_SET (tr_alive, GST_OBJECT_FLAG_MAY_BE_LEAKED);
730 tr_refings = gst_tracer_record_new ("object-refings.class",
731 RECORD_FIELD_TYPE_TS, RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS,
732 RECORD_FIELD_DESC, RECORD_FIELD_REF_COUNT, RECORD_FIELD_TRACE, NULL);
733 GST_OBJECT_FLAG_SET (tr_alive, GST_OBJECT_FLAG_MAY_BE_LEAKED);
735 if (g_getenv ("GST_LEAKS_TRACER_SIG")) {
739 g_warning ("System doesn't support POSIX signals");
740 #endif /* G_OS_UNIX */