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",stack-traces-flags=full)
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_from_string (GstLeaksTracer * self, const gchar * str)
95 /* Test if we can retrieve backtrace */
96 trace = gst_debug_get_stack_trace (FALSE);
102 if (g_strcmp0 (str, "full") == 0)
103 self->trace_flags = GST_STACK_TRACE_SHOW_FULL;
105 self->trace_flags = 0;
109 set_print_stack_trace (GstLeaksTracer * self, GstStructure * params)
111 const gchar *trace_flags = g_getenv ("GST_LEAKS_TRACER_STACK_TRACE");
113 self->trace_flags = -1;
114 if (!trace_flags && params)
115 trace_flags = gst_structure_get_string (params, "stack-traces-flags");
120 set_print_stack_trace_from_string (self, trace_flags);
124 set_filters (GstLeaksTracer * self, const gchar * filters)
127 GStrv tmp = g_strsplit (filters, ",", -1);
129 self->filter = g_array_sized_new (FALSE, FALSE, sizeof (GType),
130 g_strv_length (tmp));
131 for (i = 0; tmp[i]; i++) {
134 type = g_type_from_name (tmp[i]);
136 /* The type may not yet be known by the type system, typically because
137 * the plugin implementing it as not yet be loaded. Save it for now as
138 * it will have another chance to be added to the filter later in
139 * should_handle_object_type() when/if the object type is actually
141 if (!self->unhandled_filter)
142 self->unhandled_filter = g_hash_table_new_full (g_str_hash, g_str_equal,
145 g_hash_table_add (self->unhandled_filter, g_strdup (tmp[i]));
146 g_atomic_int_inc (&self->unhandled_filter_count);
150 GST_DEBUG_OBJECT (self, "add filter on %s", tmp[i]);
152 g_array_append_val (self->filter, type);
159 set_params_from_structure (GstLeaksTracer * self, GstStructure * params)
161 const gchar *filters = gst_structure_get_string (params, "filters");
164 set_filters (self, filters);
165 gst_structure_get_boolean (params, "check-refs", &self->check_refs);
169 set_params (GstLeaksTracer * self)
172 GstStructure *params_struct = NULL;
174 g_object_get (self, "params", ¶ms, NULL);
178 tmp = g_strdup_printf ("leaks,%s", params);
179 params_struct = gst_structure_from_string (tmp, NULL);
183 set_params_from_structure (self, params_struct);
185 set_filters (self, params);
190 set_print_stack_trace (self, params_struct);
193 gst_structure_free (params_struct);
197 _expand_unhandled_filters (gchar * typename, gpointer unused_value,
198 GstLeaksTracer * self)
202 type = g_type_from_name (typename);
207 g_atomic_int_dec_and_test (&self->unhandled_filter_count);
208 g_array_append_val (self->filter, type);
214 should_handle_object_type (GstLeaksTracer * self, GType object_type)
219 /* No filtering, handle all types */
222 if (object_type == 0)
226 if (g_atomic_int_get (&self->unhandled_filter_count)) {
227 GST_OBJECT_LOCK (self);
228 g_hash_table_foreach_remove (self->unhandled_filter,
229 (GHRFunc) _expand_unhandled_filters, self);
230 GST_OBJECT_UNLOCK (self);
233 len = self->filter->len;
234 for (i = 0; i < len; i++) {
235 GType type = g_array_index (self->filter, GType, i);
237 if (g_type_is_a (object_type, type))
245 /* The object may be destroyed when we log it using the checkpointing system so
246 * we have to save its type name */
250 const gchar *type_name;
254 object_log_new (gpointer obj)
256 ObjectLog *o = g_slice_new (ObjectLog);
260 if (G_IS_OBJECT (obj))
261 o->type_name = G_OBJECT_TYPE_NAME (obj);
263 o->type_name = g_type_name (GST_MINI_OBJECT_TYPE (obj));
269 object_log_free (ObjectLog * obj)
271 g_slice_free (ObjectLog, obj);
273 #endif /* G_OS_UNIX */
276 handle_object_destroyed (GstLeaksTracer * self, gpointer object)
278 GST_OBJECT_LOCK (self);
281 ("object %p destroyed while the leaks tracer was finalizing. Some threads are still running?",
286 g_hash_table_remove (self->objects, object);
289 g_hash_table_add (self->removed, object_log_new (object));
290 #endif /* G_OS_UNIX */
292 GST_OBJECT_UNLOCK (self);
296 object_weak_cb (gpointer data, GObject * object)
298 GstLeaksTracer *self = data;
300 handle_object_destroyed (self, object);
304 mini_object_weak_cb (gpointer data, GstMiniObject * object)
306 GstLeaksTracer *self = data;
308 handle_object_destroyed (self, object);
312 handle_object_created (GstLeaksTracer * self, gpointer object, GType type,
315 ObjectRefingInfos *infos;
318 if (!should_handle_object_type (self, type))
321 infos = g_malloc0 (sizeof (ObjectRefingInfos));
323 g_object_weak_ref ((GObject *) object, object_weak_cb, self);
325 gst_mini_object_weak_ref (GST_MINI_OBJECT_CAST (object),
326 mini_object_weak_cb, self);
328 GST_OBJECT_LOCK (self);
329 if ((gint) self->trace_flags != -1)
330 infos->creation_trace = gst_debug_get_stack_trace (self->trace_flags);
332 g_hash_table_insert (self->objects, object, infos);
336 g_hash_table_add (self->added, object_log_new (object));
337 #endif /* G_OS_UNIX */
338 GST_OBJECT_UNLOCK (self);
342 mini_object_created_cb (GstTracer * tracer, GstClockTime ts,
343 GstMiniObject * object)
345 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
347 handle_object_created (self, object, GST_MINI_OBJECT_TYPE (object), FALSE);
351 object_created_cb (GstTracer * tracer, GstClockTime ts, GstObject * object)
353 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
354 GType object_type = G_OBJECT_TYPE (object);
356 /* Can't track tracers as they may be disposed after the leak tracer itself */
357 if (g_type_is_a (object_type, GST_TYPE_TRACER))
360 handle_object_created (self, object, object_type, TRUE);
364 handle_object_reffed (GstLeaksTracer * self, gpointer object, gint new_refcount,
365 gboolean reffed, GstClockTime ts)
367 ObjectRefingInfos *infos;
368 ObjectRefingInfo *refinfo;
370 if (!self->check_refs)
373 GST_OBJECT_LOCK (self);
374 infos = g_hash_table_lookup (self->objects, object);
378 refinfo = g_malloc0 (sizeof (ObjectRefingInfo));
380 refinfo->new_refcount = new_refcount;
381 refinfo->reffed = reffed;
382 if ((gint) self->trace_flags != -1)
383 refinfo->trace = gst_debug_get_stack_trace (self->trace_flags);
385 infos->refing_infos = g_list_prepend (infos->refing_infos, refinfo);
388 GST_OBJECT_UNLOCK (self);
392 object_reffed_cb (GstTracer * tracer, GstClockTime ts, GstObject * object,
395 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
397 handle_object_reffed (self, object, new_refcount, TRUE, ts);
401 object_unreffed_cb (GstTracer * tracer, GstClockTime ts, GstObject * object,
404 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
406 handle_object_reffed (self, object, new_refcount, FALSE, ts);
410 mini_object_reffed_cb (GstTracer * tracer, GstClockTime ts,
411 GstMiniObject * object, gint new_refcount)
413 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
415 handle_object_reffed (self, object, new_refcount, TRUE, ts);
419 mini_object_unreffed_cb (GstTracer * tracer, GstClockTime ts,
420 GstMiniObject * object, gint new_refcount)
422 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
424 handle_object_reffed (self, object, new_refcount, FALSE, ts);
428 gst_leaks_tracer_init (GstLeaksTracer * self)
430 self->objects = g_hash_table_new_full (NULL, NULL, NULL,
431 (GDestroyNotify) object_refing_infos_free);
433 g_queue_push_tail (&instances, self);
437 gst_leaks_tracer_constructed (GObject * object)
439 GstLeaksTracer *self = GST_LEAKS_TRACER (object);
440 GstTracer *tracer = GST_TRACER (object);
444 gst_tracing_register_hook (tracer, "mini-object-created",
445 G_CALLBACK (mini_object_created_cb));
446 gst_tracing_register_hook (tracer, "object-created",
447 G_CALLBACK (object_created_cb));
449 if (self->check_refs) {
450 gst_tracing_register_hook (tracer, "object-reffed",
451 G_CALLBACK (object_reffed_cb));
452 gst_tracing_register_hook (tracer, "mini-object-reffed",
453 G_CALLBACK (mini_object_reffed_cb));
454 gst_tracing_register_hook (tracer, "mini-object-unreffed",
455 G_CALLBACK (mini_object_unreffed_cb));
456 gst_tracing_register_hook (tracer, "object-unreffed",
457 G_CALLBACK (object_unreffed_cb));
460 /* We rely on weak pointers rather than (mini-)object-destroyed hooks so we
461 * are notified of objects being destroyed even during the shuting down of
462 * the tracing system. */
464 ((GObjectClass *) gst_leaks_tracer_parent_class)->constructed (object);
470 const gchar *type_name;
473 ObjectRefingInfos *infos;
476 /* The content of the returned Leak struct is valid until the self->objects
477 * hash table has been modified. */
479 leak_new (gpointer obj, GType type, guint ref_count, ObjectRefingInfos * infos)
481 Leak *leak = g_slice_new (Leak);
484 leak->type_name = g_type_name (type);
485 leak->ref_count = ref_count;
486 leak->desc = gst_info_strdup_printf ("%" GST_PTR_FORMAT, obj);
493 leak_free (Leak * leak)
496 g_slice_free (Leak, leak);
500 sort_leaks (gconstpointer _a, gconstpointer _b)
502 const Leak *a = _a, *b = _b;
504 return g_strcmp0 (a->type_name, b->type_name);
508 create_leaks_list (GstLeaksTracer * self)
514 g_hash_table_iter_init (&iter, self->objects);
515 while (g_hash_table_iter_next (&iter, &obj, &infos)) {
519 if (GST_IS_OBJECT (obj)) {
520 if (GST_OBJECT_FLAG_IS_SET (obj, GST_OBJECT_FLAG_MAY_BE_LEAKED))
523 type = G_OBJECT_TYPE (obj);
524 ref_count = ((GObject *) obj)->ref_count;
526 if (GST_MINI_OBJECT_FLAG_IS_SET (obj, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED))
529 type = GST_MINI_OBJECT_TYPE (obj);
530 ref_count = ((GstMiniObject *) obj)->refcount;
533 l = g_list_prepend (l, leak_new (obj, type, ref_count, infos));
536 /* Sort leaks by type name so they are grouped together making the output
538 l = g_list_sort (l, sort_leaks);
543 /* Return TRUE if at least one leaked object has been logged */
545 log_leaked (GstLeaksTracer * self)
547 GList *ref, *leaks, *l;
549 leaks = create_leaks_list (self);
553 for (l = leaks; l != NULL; l = g_list_next (l)) {
554 Leak *leak = l->data;
556 gst_tracer_record_log (tr_alive, leak->type_name, leak->obj, leak->desc,
558 leak->infos->creation_trace ? leak->infos->creation_trace : "");
560 leak->infos->refing_infos = g_list_reverse (leak->infos->refing_infos);
561 for (ref = leak->infos->refing_infos; ref; ref = ref->next) {
562 ObjectRefingInfo *refinfo = (ObjectRefingInfo *) ref->data;
564 gst_tracer_record_log (tr_refings, refinfo->ts, leak->type_name,
565 leak->obj, refinfo->reffed ? "reffed" : "unreffed",
566 refinfo->new_refcount, refinfo->trace ? refinfo->trace : "");
570 g_list_free_full (leaks, (GDestroyNotify) leak_free);
576 gst_leaks_tracer_finalize (GObject * object)
578 GstLeaksTracer *self = GST_LEAKS_TRACER (object);
585 /* Tracers are destroyed as part of gst_deinit() so now is a good time to
586 * report all the objects which are still alive. */
587 leaks = log_leaked (self);
589 /* Remove weak references */
590 g_hash_table_iter_init (&iter, self->objects);
591 while (g_hash_table_iter_next (&iter, &obj, NULL)) {
592 if (GST_IS_OBJECT (obj))
593 g_object_weak_unref (obj, object_weak_cb, self);
595 gst_mini_object_weak_unref (GST_MINI_OBJECT_CAST (obj),
596 mini_object_weak_cb, self);
599 g_clear_pointer (&self->objects, g_hash_table_unref);
601 g_array_free (self->filter, TRUE);
602 g_clear_pointer (&self->added, g_hash_table_unref);
603 g_clear_pointer (&self->removed, g_hash_table_unref);
604 g_clear_pointer (&self->unhandled_filter, g_hash_table_unref);
606 g_queue_remove (&instances, self);
609 g_warning ("Leaks detected");
611 ((GObjectClass *) gst_leaks_tracer_parent_class)->finalize (object);
614 #define RECORD_FIELD_TYPE_TS \
615 "ts", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
616 "type", G_TYPE_GTYPE, GST_TYPE_CLOCK_TIME, \
617 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
619 #define RECORD_FIELD_TYPE_NAME \
620 "type-name", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
621 "type", G_TYPE_GTYPE, G_TYPE_STRING, \
622 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
624 #define RECORD_FIELD_ADDRESS \
625 "address", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
626 "type", G_TYPE_GTYPE, G_TYPE_POINTER, \
627 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, \
628 GST_TRACER_VALUE_SCOPE_PROCESS, \
630 #define RECORD_FIELD_DESC \
631 "description", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
632 "type", G_TYPE_GTYPE, G_TYPE_STRING, \
633 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
635 #define RECORD_FIELD_REF_COUNT \
636 "ref-count", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
637 "type", G_TYPE_GTYPE, G_TYPE_UINT, \
638 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
640 #define RECORD_FIELD_TRACE \
641 "trace", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
642 "type", G_TYPE_GTYPE, G_TYPE_STRING, \
643 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
648 sig_usr1_handler_foreach (gpointer data, gpointer user_data)
650 GstLeaksTracer *tracer = data;
652 GST_OBJECT_LOCK (tracer);
653 GST_TRACE_OBJECT (tracer, "start listing currently alive objects");
655 GST_TRACE_OBJECT (tracer, "done listing currently alive objects");
656 GST_OBJECT_UNLOCK (tracer);
660 sig_usr1_handler (G_GNUC_UNUSED int signal)
662 g_queue_foreach (&instances, sig_usr1_handler_foreach, NULL);
666 log_checkpoint (GHashTable * hash, GstTracerRecord * record)
671 g_hash_table_iter_init (&iter, hash);
672 while (g_hash_table_iter_next (&iter, &o, NULL)) {
675 gst_tracer_record_log (record, obj->type_name, obj->object);
680 do_checkpoint (GstLeaksTracer * self)
682 GST_TRACE_OBJECT (self, "listing objects created since last checkpoint");
683 log_checkpoint (self->added, tr_added);
684 GST_TRACE_OBJECT (self, "listing objects removed since last checkpoint");
685 log_checkpoint (self->removed, tr_removed);
687 g_hash_table_remove_all (self->added);
688 g_hash_table_remove_all (self->removed);
692 sig_usr2_handler_foreach (gpointer data, gpointer user_data)
694 GstLeaksTracer *tracer = data;
696 GST_OBJECT_LOCK (tracer);
698 if (!tracer->added) {
699 GST_TRACE_OBJECT (tracer, "First checkpoint, start tracking objects");
701 tracer->added = g_hash_table_new_full (NULL, NULL,
702 (GDestroyNotify) object_log_free, NULL);
703 tracer->removed = g_hash_table_new_full (NULL, NULL,
704 (GDestroyNotify) object_log_free, NULL);
706 do_checkpoint (tracer);
709 GST_OBJECT_UNLOCK (tracer);
713 sig_usr2_handler (G_GNUC_UNUSED int signal)
715 g_queue_foreach (&instances, sig_usr2_handler_foreach, NULL);
721 tr_added = gst_tracer_record_new ("object-added.class",
722 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, NULL);
723 GST_OBJECT_FLAG_SET (tr_added, GST_OBJECT_FLAG_MAY_BE_LEAKED);
725 tr_removed = gst_tracer_record_new ("object-removed.class",
726 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, NULL);
727 GST_OBJECT_FLAG_SET (tr_removed, GST_OBJECT_FLAG_MAY_BE_LEAKED);
729 signal (SIGUSR1, sig_usr1_handler);
730 signal (SIGUSR2, sig_usr2_handler);
732 #endif /* G_OS_UNIX */
735 gst_leaks_tracer_class_init (GstLeaksTracerClass * klass)
737 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
739 gobject_class->constructed = gst_leaks_tracer_constructed;
740 gobject_class->finalize = gst_leaks_tracer_finalize;
742 tr_alive = gst_tracer_record_new ("object-alive.class",
743 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, RECORD_FIELD_DESC,
744 RECORD_FIELD_REF_COUNT, RECORD_FIELD_TRACE, NULL);
745 GST_OBJECT_FLAG_SET (tr_alive, GST_OBJECT_FLAG_MAY_BE_LEAKED);
747 tr_refings = gst_tracer_record_new ("object-refings.class",
748 RECORD_FIELD_TYPE_TS, RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS,
749 RECORD_FIELD_DESC, RECORD_FIELD_REF_COUNT, RECORD_FIELD_TRACE, NULL);
750 GST_OBJECT_FLAG_SET (tr_alive, GST_OBJECT_FLAG_MAY_BE_LEAKED);
752 if (g_getenv ("GST_LEAKS_TRACER_SIG")) {
756 g_warning ("System doesn't support POSIX signals");
757 #endif /* G_OS_UNIX */