2 * Copyright (C) 2016 Collabora Ltd. <guillaume.desmottes@collabora.co.uk>
3 * Copyright (C) 2019 Nirbheek Chauhan <nirbheek@centricular.com>
5 * gstleaks.c: tracing module detecting object leaks
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 * SECTION:tracer-leaks
24 * @short_description: detect GstObject and GstMiniObject leaks
26 * This tracing module tracks the lifetimes of #GstObject and #GstMiniObject
27 * objects and prints a list of leaks to the debug log under `GST_TRACER:7` when
28 * gst_deinit() is called, and also prints a g_warning().
30 * Starting with GStreamer 1.18, you can also use action signals on the tracer
31 * object to fetch leak information. Use gst_tracing_get_active_tracers() to
32 * get a list of all active tracers and find the right one by name.
34 * You can activate this tracer in the usual way by adding the string 'leaks'
35 * to the environment variable `GST_TRACERS`. Such as: `GST_TRACERS=leaks`
37 * Note that the values are separated by semicolon (`;`), such as:
38 * `GST_TRACERS=leaks;latency`, and multiple instances of the same tracer can be
39 * active at the same time.
41 * Parameters can also be passed to each tracer. The leaks tracer currently
42 * accepts five params:
43 * 1. filters: (string) to filter which objects to record
44 * 2. check-refs: (boolean) whether to record every location where a leaked
45 * object was reffed and unreffed
46 * 3. stack-traces-flags: (string) full or none; see: #GstStackTraceFlags
47 * 4. name: (string) set a name for the tracer object itself
48 * 5. log-leaks-on-deinit: (boolean) whether to report all leaks on
49 * gst_deinit() by printing them in the debug log; "true" by default
53 * GST_TRACERS='leaks(filters="GstEvent,GstMessage",stack-traces-flags=none)'
56 * GST_TRACERS='leaks(filters="GstBuffer",stack-traces-flags=full,check-refs=true);leaks(name=all-leaks)'
67 #include <glib-unix.h>
69 #endif /* G_OS_UNIX */
71 GST_DEBUG_CATEGORY_STATIC (gst_leaks_debug);
72 #define GST_CAT_DEFAULT gst_leaks_debug
77 SIGNAL_GET_LIVE_OBJECTS,
78 SIGNAL_LOG_LIVE_OBJECTS,
79 SIGNAL_ACTIVITY_START_TRACKING,
80 SIGNAL_ACTIVITY_GET_CHECKPOINT,
81 SIGNAL_ACTIVITY_LOG_CHECKPOINT,
82 SIGNAL_ACTIVITY_STOP_TRACKING,
87 #define DEFAULT_LOG_LEAKS TRUE /* for backwards-compat */
90 GST_DEBUG_CATEGORY_INIT (gst_leaks_debug, "leaks", 0, "leaks tracer");
91 #define gst_leaks_tracer_parent_class parent_class
92 G_DEFINE_TYPE_WITH_CODE (GstLeaksTracer, gst_leaks_tracer,
93 GST_TYPE_TRACER, _do_init);
95 static GstStructure *gst_leaks_tracer_get_live_objects (GstLeaksTracer * self);
96 static void gst_leaks_tracer_log_live_objects (GstLeaksTracer * self);
97 static void gst_leaks_tracer_activity_start_tracking (GstLeaksTracer * self);
98 static GstStructure *gst_leaks_tracer_activity_get_checkpoint (GstLeaksTracer *
100 static void gst_leaks_tracer_activity_log_checkpoint (GstLeaksTracer * self);
101 static void gst_leaks_tracer_activity_stop_tracking (GstLeaksTracer * self);
104 static void gst_leaks_tracer_setup_signals (GstLeaksTracer * leaks);
105 static void gst_leaks_tracer_cleanup_signals (GstLeaksTracer * leaks);
108 static GstTracerRecord *tr_alive;
109 static GstTracerRecord *tr_refings;
110 static GstTracerRecord *tr_added = NULL;
111 static GstTracerRecord *tr_removed = NULL;
112 static GQueue instances = G_QUEUE_INIT;
113 static guint gst_leaks_tracer_signals[LAST_SIGNAL] = { 0 };
115 G_LOCK_DEFINE_STATIC (instances);
127 gchar *creation_trace;
133 object_refing_info_free (ObjectRefingInfo * refinfo)
135 g_free (refinfo->trace);
140 object_refing_infos_free (ObjectRefingInfos * infos)
142 g_list_free_full (infos->refing_infos,
143 (GDestroyNotify) object_refing_info_free);
145 g_free (infos->creation_trace);
150 set_print_stack_trace_from_string (GstLeaksTracer * self, const gchar * str)
154 /* Test if we can retrieve backtrace */
155 trace = gst_debug_get_stack_trace (FALSE);
161 if (g_strcmp0 (str, "full") == 0)
162 self->trace_flags = GST_STACK_TRACE_SHOW_FULL;
164 self->trace_flags = GST_STACK_TRACE_SHOW_NONE;
168 set_print_stack_trace (GstLeaksTracer * self, GstStructure * params)
170 const gchar *trace_flags = g_getenv ("GST_LEAKS_TRACER_STACK_TRACE");
172 self->trace_flags = -1;
173 if (!trace_flags && params)
174 trace_flags = gst_structure_get_string (params, "stack-traces-flags");
179 set_print_stack_trace_from_string (self, trace_flags);
183 set_filters (GstLeaksTracer * self, const gchar * filters)
186 GStrv tmp = g_strsplit (filters, ",", -1);
188 self->filter = g_array_sized_new (FALSE, FALSE, sizeof (GType),
189 g_strv_length (tmp));
190 for (i = 0; tmp[i]; i++) {
193 type = g_type_from_name (tmp[i]);
195 /* The type may not yet be known by the type system, typically because
196 * the plugin implementing it as not yet be loaded. Save it for now as
197 * it will have another chance to be added to the filter later in
198 * should_handle_object_type() when/if the object type is actually
200 if (!self->unhandled_filter)
201 self->unhandled_filter = g_hash_table_new_full (g_str_hash, g_str_equal,
204 g_hash_table_add (self->unhandled_filter, g_strdup (tmp[i]));
205 g_atomic_int_inc (&self->unhandled_filter_count);
209 GST_DEBUG_OBJECT (self, "add filter on %s", tmp[i]);
211 g_array_append_val (self->filter, type);
218 set_params_from_structure (GstLeaksTracer * self, GstStructure * params)
220 const gchar *filters, *name;
222 filters = gst_structure_get_string (params, "filters");
224 set_filters (self, filters);
226 name = gst_structure_get_string (params, "name");
228 gst_object_set_name (GST_OBJECT (self), name);
230 gst_structure_get_boolean (params, "check-refs", &self->check_refs);
231 gst_structure_get_boolean (params, "log-leaks-on-deinit", &self->log_leaks);
235 set_params (GstLeaksTracer * self)
238 GstStructure *params_struct = NULL;
240 g_object_get (self, "params", ¶ms, NULL);
244 tmp = g_strdup_printf ("leaks,%s", params);
245 params_struct = gst_structure_from_string (tmp, NULL);
249 set_params_from_structure (self, params_struct);
251 set_filters (self, params);
256 set_print_stack_trace (self, params_struct);
259 gst_structure_free (params_struct);
263 _expand_unhandled_filters (gchar * typename, gpointer unused_value,
264 GstLeaksTracer * self)
268 type = g_type_from_name (typename);
273 g_atomic_int_dec_and_test (&self->unhandled_filter_count);
274 g_array_append_val (self->filter, type);
280 should_handle_object_type (GstLeaksTracer * self, GType object_type)
285 /* No filtering, handle all types */
288 if (object_type == 0)
292 if (g_atomic_int_get (&self->unhandled_filter_count)) {
293 GST_OBJECT_LOCK (self);
294 g_hash_table_foreach_remove (self->unhandled_filter,
295 (GHRFunc) _expand_unhandled_filters, self);
296 GST_OBJECT_UNLOCK (self);
299 len = self->filter->len;
300 for (i = 0; i < len; i++) {
301 GType type = g_array_index (self->filter, GType, i);
303 if (g_type_is_a (object_type, type))
310 /* The object may be destroyed when we log it using the checkpointing system so
311 * we have to save its type name */
315 const gchar *type_name;
318 static inline gboolean
319 object_is_gst_mini_object (gpointer obj)
321 return (G_TYPE_IS_DERIVED (GST_MINI_OBJECT_TYPE (obj)) &&
322 G_TYPE_FUNDAMENTAL (GST_MINI_OBJECT_TYPE (obj)) == G_TYPE_BOXED);
326 object_log_new (gpointer obj)
328 ObjectLog *o = g_new (ObjectLog, 1);
332 if (object_is_gst_mini_object (obj))
333 o->type_name = g_type_name (GST_MINI_OBJECT_TYPE (obj));
335 o->type_name = G_OBJECT_TYPE_NAME (obj);
341 object_log_free (ObjectLog * obj)
347 handle_object_destroyed (GstLeaksTracer * self, gpointer object)
349 GST_OBJECT_LOCK (self);
352 ("object %p destroyed while the leaks tracer was finalizing. Some threads are still running?",
357 g_hash_table_remove (self->objects, object);
359 g_hash_table_add (self->removed, object_log_new (object));
361 GST_OBJECT_UNLOCK (self);
365 object_weak_cb (gpointer data, GObject * object)
367 GstLeaksTracer *self = data;
369 handle_object_destroyed (self, object);
373 mini_object_weak_cb (gpointer data, GstMiniObject * object)
375 GstLeaksTracer *self = data;
377 handle_object_destroyed (self, object);
381 handle_object_created (GstLeaksTracer * self, gpointer object, GType type,
384 ObjectRefingInfos *infos;
387 if (!should_handle_object_type (self, type))
390 infos = g_malloc0 (sizeof (ObjectRefingInfos));
392 g_object_weak_ref ((GObject *) object, object_weak_cb, self);
394 gst_mini_object_weak_ref (GST_MINI_OBJECT_CAST (object),
395 mini_object_weak_cb, self);
397 GST_OBJECT_LOCK (self);
398 if ((gint) self->trace_flags != -1)
399 infos->creation_trace = gst_debug_get_stack_trace (self->trace_flags);
401 g_hash_table_insert (self->objects, object, infos);
404 g_hash_table_add (self->added, object_log_new (object));
405 GST_OBJECT_UNLOCK (self);
409 mini_object_created_cb (GstTracer * tracer, GstClockTime ts,
410 GstMiniObject * object)
412 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
414 handle_object_created (self, object, GST_MINI_OBJECT_TYPE (object), FALSE);
418 object_created_cb (GstTracer * tracer, GstClockTime ts, GstObject * object)
420 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
421 GType object_type = G_OBJECT_TYPE (object);
423 /* Can't track tracers as they may be disposed after the leak tracer itself */
424 if (g_type_is_a (object_type, GST_TYPE_TRACER))
427 handle_object_created (self, object, object_type, TRUE);
431 handle_object_reffed (GstLeaksTracer * self, gpointer object, GType type,
432 gint new_refcount, gboolean reffed, GstClockTime ts)
434 ObjectRefingInfos *infos;
435 ObjectRefingInfo *refinfo;
437 if (!self->check_refs)
440 if (!should_handle_object_type (self, type))
443 GST_OBJECT_LOCK (self);
444 infos = g_hash_table_lookup (self->objects, object);
448 refinfo = g_malloc0 (sizeof (ObjectRefingInfo));
450 refinfo->new_refcount = new_refcount;
451 refinfo->reffed = reffed;
452 if ((gint) self->trace_flags != -1)
453 refinfo->trace = gst_debug_get_stack_trace (self->trace_flags);
455 infos->refing_infos = g_list_prepend (infos->refing_infos, refinfo);
458 GST_OBJECT_UNLOCK (self);
462 object_reffed_cb (GstTracer * tracer, GstClockTime ts, GstObject * object,
465 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
467 handle_object_reffed (self, object, G_OBJECT_TYPE (object), new_refcount,
472 object_unreffed_cb (GstTracer * tracer, GstClockTime ts, GstObject * object,
475 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
477 handle_object_reffed (self, object, G_OBJECT_TYPE (object), new_refcount,
482 mini_object_reffed_cb (GstTracer * tracer, GstClockTime ts,
483 GstMiniObject * object, gint new_refcount)
485 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
487 handle_object_reffed (self, object, GST_MINI_OBJECT_TYPE (object),
488 new_refcount, TRUE, ts);
492 mini_object_unreffed_cb (GstTracer * tracer, GstClockTime ts,
493 GstMiniObject * object, gint new_refcount)
495 GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
497 handle_object_reffed (self, object, GST_MINI_OBJECT_TYPE (object),
498 new_refcount, FALSE, ts);
502 gst_leaks_tracer_init (GstLeaksTracer * self)
504 self->log_leaks = DEFAULT_LOG_LEAKS;
505 self->objects = g_hash_table_new_full (NULL, NULL, NULL,
506 (GDestroyNotify) object_refing_infos_free);
508 if (g_getenv ("GST_LEAKS_TRACER_SIG")) {
510 gst_leaks_tracer_setup_signals (self);
512 g_warning ("System doesn't support POSIX signals");
513 #endif /* G_OS_UNIX */
517 g_queue_push_tail (&instances, self);
518 G_UNLOCK (instances);
522 gst_leaks_tracer_constructed (GObject * object)
524 GstLeaksTracer *self = GST_LEAKS_TRACER (object);
525 GstTracer *tracer = GST_TRACER (object);
529 gst_tracing_register_hook (tracer, "mini-object-created",
530 G_CALLBACK (mini_object_created_cb));
531 gst_tracing_register_hook (tracer, "object-created",
532 G_CALLBACK (object_created_cb));
534 if (self->check_refs) {
535 gst_tracing_register_hook (tracer, "object-reffed",
536 G_CALLBACK (object_reffed_cb));
537 gst_tracing_register_hook (tracer, "mini-object-reffed",
538 G_CALLBACK (mini_object_reffed_cb));
539 gst_tracing_register_hook (tracer, "mini-object-unreffed",
540 G_CALLBACK (mini_object_unreffed_cb));
541 gst_tracing_register_hook (tracer, "object-unreffed",
542 G_CALLBACK (object_unreffed_cb));
545 /* We rely on weak pointers rather than (mini-)object-destroyed hooks so we
546 * are notified of objects being destroyed even during the shuting down of
547 * the tracing system. */
549 ((GObjectClass *) gst_leaks_tracer_parent_class)->constructed (object);
558 ObjectRefingInfos *infos;
561 /* The content of the returned Leak struct is valid until the self->objects
562 * hash table has been modified. */
564 leak_new (gpointer obj, GType type, guint ref_count, ObjectRefingInfos * infos)
566 Leak *leak = g_new (Leak, 1);
570 leak->ref_count = ref_count;
571 leak->desc = gst_info_strdup_printf ("%" GST_PTR_FORMAT, obj);
578 leak_free (Leak * leak)
585 sort_leaks (gconstpointer _a, gconstpointer _b)
587 const Leak *a = _a, *b = _b;
589 return g_strcmp0 (g_type_name (a->type), g_type_name (b->type));
593 create_leaks_list (GstLeaksTracer * self)
599 g_hash_table_iter_init (&iter, self->objects);
600 while (g_hash_table_iter_next (&iter, &obj, &infos)) {
604 if (object_is_gst_mini_object (obj)) {
605 if (GST_MINI_OBJECT_FLAG_IS_SET (obj, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED))
608 type = GST_MINI_OBJECT_TYPE (obj);
609 ref_count = ((GstMiniObject *) obj)->refcount;
611 if (GST_OBJECT_FLAG_IS_SET (obj, GST_OBJECT_FLAG_MAY_BE_LEAKED))
614 type = G_OBJECT_TYPE (obj);
615 ref_count = ((GObject *) obj)->ref_count;
618 l = g_list_prepend (l, leak_new (obj, type, ref_count, infos));
621 /* Sort leaks by type name so they are grouped together making the output
623 l = g_list_sort (l, sort_leaks);
625 /* Reverse list to sort objects by creation time; this is needed because we
626 * prepended objects into this list earlier, and because g_list_sort() above
627 * is stable so the creation order is preserved when sorting by type name. */
628 return g_list_reverse (l);
632 process_leak (Leak * leak, GValue * ret_leaks)
634 GstStructure *r, *s = NULL;
636 GValue refings = G_VALUE_INIT;
639 /* log to the debug log */
640 gst_tracer_record_log (tr_alive, g_type_name (leak->type), leak->obj,
641 leak->desc, leak->ref_count,
642 leak->infos->creation_trace ? leak->infos->creation_trace : "");
644 GValue s_value = G_VALUE_INIT;
645 GValue obj_value = G_VALUE_INIT;
646 /* for leaked objects, we take ownership of the object instead of
647 * reffing ("collecting") it to avoid deadlocks */
648 g_value_init (&obj_value, leak->type);
649 if (object_is_gst_mini_object (leak->obj))
650 g_value_take_boxed (&obj_value, leak->obj);
652 g_value_take_object (&obj_value, leak->obj);
653 s = gst_structure_new_empty ("object-alive");
654 gst_structure_take_value (s, "object", &obj_value);
655 gst_structure_set (s, "ref-count", G_TYPE_UINT, leak->ref_count,
656 "trace", G_TYPE_STRING, leak->infos->creation_trace, NULL);
657 /* avoid copy of structure */
658 g_value_init (&s_value, GST_TYPE_STRUCTURE);
659 g_value_take_boxed (&s_value, s);
660 gst_value_list_append_and_take_value (ret_leaks, &s_value);
663 /* store refinfo if available */
664 if (leak->infos->refing_infos)
665 g_value_init (&refings, GST_TYPE_LIST);
667 /* iterate the list from last to first to correct the order */
668 for (ref = g_list_last (leak->infos->refing_infos); ref; ref = ref->prev) {
669 ObjectRefingInfo *refinfo = (ObjectRefingInfo *) ref->data;
672 /* log to the debug log */
673 gst_tracer_record_log (tr_refings, refinfo->ts, g_type_name (leak->type),
674 leak->obj, refinfo->reffed ? "reffed" : "unreffed",
675 refinfo->new_refcount, refinfo->trace ? refinfo->trace : "");
677 GValue r_value = G_VALUE_INIT;
678 r = gst_structure_new_empty ("object-refings");
679 gst_structure_set (r, "ts", GST_TYPE_CLOCK_TIME, refinfo->ts,
680 "desc", G_TYPE_STRING, refinfo->reffed ? "reffed" : "unreffed",
681 "ref-count", G_TYPE_UINT, refinfo->new_refcount,
682 "trace", G_TYPE_STRING, refinfo->trace, NULL);
683 /* avoid copy of structure */
684 g_value_init (&r_value, GST_TYPE_STRUCTURE);
685 g_value_take_boxed (&r_value, r);
686 gst_value_list_append_and_take_value (&refings, &r_value);
690 if (ret_leaks && leak->infos->refing_infos)
691 gst_structure_take_value (s, "ref-infos", &refings);
694 /* Return TRUE if at least one leaked object was found */
696 process_leaks (GstLeaksTracer * self, GValue * ret_leaks)
699 gboolean ret = FALSE;
702 GST_TRACE_OBJECT (self, "start listing currently alive objects");
704 leaks = create_leaks_list (self);
707 GST_TRACE_OBJECT (self, "No objects alive currently");
711 for (l = leaks; l; l = l->next)
712 process_leak (l->data, ret_leaks);
714 g_list_free_full (leaks, (GDestroyNotify) leak_free);
720 GST_TRACE_OBJECT (self, "done listing currently alive objects");
726 gst_leaks_tracer_finalize (GObject * object)
728 GstLeaksTracer *self = GST_LEAKS_TRACER (object);
729 gboolean leaks = FALSE;
733 GST_DEBUG_OBJECT (self, "destroying tracer, checking for leaks");
737 /* Tracers are destroyed as part of gst_deinit() so now is a good time to
738 * report all the objects which are still alive. */
740 leaks = process_leaks (self, NULL);
742 /* Remove weak references */
743 g_hash_table_iter_init (&iter, self->objects);
744 while (g_hash_table_iter_next (&iter, &obj, NULL)) {
745 if (object_is_gst_mini_object (obj))
746 gst_mini_object_weak_unref (GST_MINI_OBJECT_CAST (obj),
747 mini_object_weak_cb, self);
749 g_object_weak_unref (obj, object_weak_cb, self);
752 g_clear_pointer (&self->objects, g_hash_table_unref);
754 g_array_free (self->filter, TRUE);
755 g_clear_pointer (&self->added, g_hash_table_unref);
756 g_clear_pointer (&self->removed, g_hash_table_unref);
757 g_clear_pointer (&self->unhandled_filter, g_hash_table_unref);
760 g_queue_remove (&instances, self);
761 G_UNLOCK (instances);
764 gst_leaks_tracer_cleanup_signals (self);
768 g_warning ("Leaks detected and logged under GST_DEBUG=GST_TRACER:7");
770 ((GObjectClass *) gst_leaks_tracer_parent_class)->finalize (object);
773 #define RECORD_FIELD_TYPE_TS \
774 "ts", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
775 "type", G_TYPE_GTYPE, GST_TYPE_CLOCK_TIME, \
777 #define RECORD_FIELD_TYPE_NAME \
778 "type-name", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
779 "type", G_TYPE_GTYPE, G_TYPE_STRING, \
781 #define RECORD_FIELD_ADDRESS \
782 "address", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
783 "type", G_TYPE_GTYPE, G_TYPE_POINTER, \
785 #define RECORD_FIELD_DESC \
786 "description", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
787 "type", G_TYPE_GTYPE, G_TYPE_STRING, \
789 #define RECORD_FIELD_REF_COUNT \
790 "ref-count", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
791 "type", G_TYPE_GTYPE, G_TYPE_UINT, \
793 #define RECORD_FIELD_TRACE \
794 "trace", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
795 "type", G_TYPE_GTYPE, G_TYPE_STRING, \
800 sig_usr1_handler (gpointer data)
803 g_queue_foreach (&instances, (GFunc) gst_leaks_tracer_log_live_objects, NULL);
804 G_UNLOCK (instances);
806 return G_SOURCE_CONTINUE;
810 sig_usr2_handler_foreach (gpointer data, gpointer user_data)
812 GstLeaksTracer *tracer = data;
814 if (!tracer->added) {
815 GST_TRACE_OBJECT (tracer, "First checkpoint, start tracking objects");
816 gst_leaks_tracer_activity_start_tracking (tracer);
818 gst_leaks_tracer_activity_log_checkpoint (tracer);
823 sig_usr2_handler (gpointer data)
826 g_queue_foreach (&instances, sig_usr2_handler_foreach, NULL);
827 G_UNLOCK (instances);
829 return G_SOURCE_CONTINUE;
832 struct signal_thread_data
839 static GMainLoop *signal_loop; /* NULL */
840 static GThread *signal_thread; /* NULL */
841 static gint signal_thread_users; /* 0 */
842 G_LOCK_DEFINE_STATIC (signal_thread);
845 unlock_mutex (gpointer data)
847 g_mutex_unlock ((GMutex *) data);
849 return G_SOURCE_REMOVE;
853 gst_leaks_tracer_signal_thread (struct signal_thread_data *data)
855 static GMainContext *signal_ctx;
856 GSource *source1, *source2, *unlock_source;
858 signal_ctx = g_main_context_new ();
859 signal_loop = g_main_loop_new (signal_ctx, FALSE);
861 unlock_source = g_idle_source_new ();
862 g_source_set_callback (unlock_source, unlock_mutex, &data->lock, NULL);
863 g_source_attach (unlock_source, signal_ctx);
865 source1 = g_unix_signal_source_new (SIGUSR1);
866 g_source_set_callback (source1, sig_usr1_handler, NULL, NULL);
867 g_source_attach (source1, signal_ctx);
869 source2 = g_unix_signal_source_new (SIGUSR2);
870 g_source_set_callback (source2, sig_usr2_handler, NULL, NULL);
871 g_source_attach (source2, signal_ctx);
873 g_mutex_lock (&data->lock);
875 g_cond_broadcast (&data->cond);
877 g_main_loop_run (signal_loop);
879 g_source_destroy (source1);
880 g_source_destroy (source2);
881 g_main_loop_unref (signal_loop);
883 g_main_context_unref (signal_ctx);
890 atfork_prepare (void)
892 G_LOCK (signal_thread);
898 G_UNLOCK (signal_thread);
904 signal_thread_users = 0;
905 signal_thread = NULL;
906 G_UNLOCK (signal_thread);
910 gst_leaks_tracer_setup_signals (GstLeaksTracer * leaks)
912 struct signal_thread_data data;
914 G_LOCK (signal_thread);
915 signal_thread_users++;
916 if (signal_thread_users == 1) {
919 GST_INFO_OBJECT (leaks, "Setting up signal handling");
921 /* If application is forked, the child process won't inherit the extra thread.
922 * As a result we need to reset the child process thread state accordingly.
923 * This is typically needed when running tests as libcheck fork the tests.
925 * See https://pubs.opengroup.org/onlinepubs/007904975/functions/pthread_atfork.html
927 res = pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
929 GST_WARNING_OBJECT (leaks, "pthread_atfork() failed (%d)", res);
933 g_mutex_init (&data.lock);
934 g_cond_init (&data.cond);
935 signal_thread = g_thread_new ("gstleak-signal",
936 (GThreadFunc) gst_leaks_tracer_signal_thread, &data);
938 g_mutex_lock (&data.lock);
940 g_cond_wait (&data.cond, &data.lock);
941 g_mutex_unlock (&data.lock);
943 g_mutex_clear (&data.lock);
944 g_cond_clear (&data.cond);
946 G_UNLOCK (signal_thread);
950 gst_leaks_tracer_cleanup_signals (GstLeaksTracer * leaks)
952 G_LOCK (signal_thread);
953 signal_thread_users--;
954 if (signal_thread_users == 0) {
955 GST_INFO_OBJECT (leaks, "Cleaning up signal handling");
956 g_main_loop_quit (signal_loop);
957 g_thread_join (signal_thread);
958 signal_thread = NULL;
959 gst_object_unref (tr_added);
961 gst_object_unref (tr_removed);
964 G_UNLOCK (signal_thread);
968 #define setup_signals() g_warning ("System doesn't support POSIX signals");
969 #endif /* G_OS_UNIX */
971 static GstStructure *
972 gst_leaks_tracer_get_live_objects (GstLeaksTracer * self)
975 GValue live_objects = G_VALUE_INIT;
977 g_value_init (&live_objects, GST_TYPE_LIST);
979 GST_OBJECT_LOCK (self);
980 process_leaks (self, &live_objects);
981 GST_OBJECT_UNLOCK (self);
983 info = gst_structure_new_empty ("live-objects-info");
984 gst_structure_take_value (info, "live-objects-list", &live_objects);
990 gst_leaks_tracer_log_live_objects (GstLeaksTracer * self)
992 GST_OBJECT_LOCK (self);
993 process_leaks (self, NULL);
994 GST_OBJECT_UNLOCK (self);
998 gst_leaks_tracer_activity_start_tracking (GstLeaksTracer * self)
1000 GST_OBJECT_LOCK (self);
1002 GST_ERROR_OBJECT (self, "tracking is already in progress");
1006 self->added = g_hash_table_new_full (NULL, NULL,
1007 (GDestroyNotify) object_log_free, NULL);
1008 self->removed = g_hash_table_new_full (NULL, NULL,
1009 (GDestroyNotify) object_log_free, NULL);
1010 GST_OBJECT_UNLOCK (self);
1013 /* When @ret is %NULL, this simply logs the activities */
1015 process_checkpoint (GstTracerRecord * record, const gchar * record_type,
1016 GHashTable * hash, GValue * ret)
1018 GHashTableIter iter;
1021 g_hash_table_iter_init (&iter, hash);
1022 while (g_hash_table_iter_next (&iter, &o, NULL)) {
1026 /* log to the debug log */
1027 gst_tracer_record_log (record, obj->type_name, obj->object);
1029 GValue s_value = G_VALUE_INIT;
1030 GValue addr_value = G_VALUE_INIT;
1031 gchar *address = g_strdup_printf ("%p", obj->object);
1032 GstStructure *s = gst_structure_new_empty (record_type);
1033 /* copy type_name because it's owned by @obj */
1034 gst_structure_set (s, "type-name", G_TYPE_STRING, obj->type_name, NULL);
1035 /* avoid copy of @address */
1036 g_value_init (&addr_value, G_TYPE_STRING);
1037 g_value_take_string (&addr_value, address);
1038 gst_structure_take_value (s, "address", &addr_value);
1039 /* avoid copy of the structure */
1040 g_value_init (&s_value, GST_TYPE_STRUCTURE);
1041 g_value_take_boxed (&s_value, s);
1042 gst_value_list_append_and_take_value (ret, &s_value);
1047 static GstStructure *
1048 gst_leaks_tracer_activity_get_checkpoint (GstLeaksTracer * self)
1050 GValue added = G_VALUE_INIT;
1051 GValue removed = G_VALUE_INIT;
1052 GstStructure *s = gst_structure_new_empty ("activity-checkpoint");
1054 g_value_init (&added, GST_TYPE_LIST);
1055 g_value_init (&removed, GST_TYPE_LIST);
1057 GST_OBJECT_LOCK (self);
1058 process_checkpoint (tr_added, "objects-created", self->added, &added);
1059 process_checkpoint (tr_removed, "objects-removed", self->removed, &removed);
1061 g_hash_table_remove_all (self->added);
1062 g_hash_table_remove_all (self->removed);
1063 GST_OBJECT_UNLOCK (self);
1065 gst_structure_take_value (s, "objects-created-list", &added);
1066 gst_structure_take_value (s, "objects-removed-list", &removed);
1072 gst_leaks_tracer_activity_log_checkpoint (GstLeaksTracer * self)
1074 GST_OBJECT_LOCK (self);
1075 GST_TRACE_OBJECT (self, "listing objects created since last checkpoint");
1076 process_checkpoint (tr_added, NULL, self->added, NULL);
1077 GST_TRACE_OBJECT (self, "listing objects removed since last checkpoint");
1078 process_checkpoint (tr_removed, NULL, self->removed, NULL);
1079 g_hash_table_remove_all (self->added);
1080 g_hash_table_remove_all (self->removed);
1081 GST_OBJECT_UNLOCK (self);
1085 gst_leaks_tracer_activity_stop_tracking (GstLeaksTracer * self)
1087 GST_OBJECT_LOCK (self);
1088 g_clear_pointer (&self->added, g_hash_table_destroy);
1089 g_clear_pointer (&self->removed, g_hash_table_destroy);
1090 GST_OBJECT_UNLOCK (self);
1094 gst_leaks_tracer_class_init (GstLeaksTracerClass * klass)
1096 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1098 gobject_class->constructed = gst_leaks_tracer_constructed;
1099 gobject_class->finalize = gst_leaks_tracer_finalize;
1101 tr_alive = gst_tracer_record_new ("object-alive.class",
1102 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, RECORD_FIELD_DESC,
1103 RECORD_FIELD_REF_COUNT, RECORD_FIELD_TRACE, NULL);
1104 GST_OBJECT_FLAG_SET (tr_alive, GST_OBJECT_FLAG_MAY_BE_LEAKED);
1106 tr_refings = gst_tracer_record_new ("object-refings.class",
1107 RECORD_FIELD_TYPE_TS, RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS,
1108 RECORD_FIELD_DESC, RECORD_FIELD_REF_COUNT, RECORD_FIELD_TRACE, NULL);
1109 GST_OBJECT_FLAG_SET (tr_alive, GST_OBJECT_FLAG_MAY_BE_LEAKED);
1111 tr_added = gst_tracer_record_new ("object-added.class",
1112 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, NULL);
1113 GST_OBJECT_FLAG_SET (tr_added, GST_OBJECT_FLAG_MAY_BE_LEAKED);
1115 tr_removed = gst_tracer_record_new ("object-removed.class",
1116 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, NULL);
1117 GST_OBJECT_FLAG_SET (tr_removed, GST_OBJECT_FLAG_MAY_BE_LEAKED);
1120 * GstLeaksTracer::get-live-objects:
1121 * @leakstracer: the leaks tracer object to emit this signal on
1123 * Returns a #GstStructure containing a #GValue of type #GST_TYPE_LIST which
1124 * is a list of #GstStructure objects containing information about the
1125 * objects that are still alive, which is useful for detecting leaks. Each
1126 * #GstStructure object has the following fields:
1128 * `object`: containing the leaked object itself
1129 * `ref-count`: the current reference count of the object
1130 * `trace`: the allocation stack trace for the object, only available if the
1131 * `stack-traces-flags` param is set to `full`
1132 * `ref-infos`: a #GValue of type #GST_TYPE_LIST which is a list of
1133 * #GstStructure objects containing information about the
1134 * ref/unref history of the object; only available if the
1135 * `check-refs` param is set to `true`
1137 * Each `ref-infos` #GstStructure has the following fields:
1139 * `ts`: the timestamp for the ref/unref
1140 * `desc`: either "reffed" or "unreffed"
1141 * `ref-count`: the reference count after the ref/unref
1142 * `trace`: the stack trace for the ref/unref
1144 * **Notes on usage**: This action signal is supposed to be called at the
1145 * end of an application before it exits, or at the end of an execution run
1146 * when all streaming has stopped and all pipelines have been freed. It is
1147 * assumed that at this point any GStreamer object that is still alive is
1148 * leaked, and there are no legitimate owners any more. As such, ownership
1149 * of the leaked objects is transferred to you then, assuming no other code
1150 * still retrains references to them.
1152 * If that's not the case, and there is code somewhere still holding
1153 * a reference, then the application behaviour is undefined after this
1154 * function is called, since we will have stolen some other code's valid
1155 * reference and when the returned #GstStructure is freed that code will be
1156 * holding a reference to an invalid object, which will most likely crash
1159 * If you don't want to just check for leaks at the end of a program, the
1160 * activity checkpoint action signals might be a better fit for your use
1163 * Returns: (transfer full): a newly-allocated #GstStructure
1167 gst_leaks_tracer_signals[SIGNAL_GET_LIVE_OBJECTS] =
1168 g_signal_new ("get-live-objects", G_TYPE_FROM_CLASS (klass),
1169 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstLeaksTracerClass,
1170 get_live_objects), NULL, NULL, NULL, GST_TYPE_STRUCTURE, 0,
1174 * GstLeaksTracer::log-live-objects:
1175 * @leakstracer: the leaks tracer object to emit this signal on
1177 * Logs all objects that are still alive to the debug log in the same format
1178 * as the logging during gst_deinit().
1182 gst_leaks_tracer_signals[SIGNAL_LOG_LIVE_OBJECTS] =
1183 g_signal_new ("log-live-objects", G_TYPE_FROM_CLASS (klass),
1184 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstLeaksTracerClass,
1185 log_live_objects), NULL, NULL, NULL, G_TYPE_NONE, 0, G_TYPE_NONE);
1188 * GstLeaksTracer:::activity-start-tracking
1189 * @leakstracer: the leaks tracer object to emit this signal on
1191 * Start storing information about all objects that are being created or
1192 * removed. Call `stop-tracking` to stop.
1194 * NOTE: You do not need to call this to use the *-live-objects action
1195 * signals listed above.
1199 gst_leaks_tracer_signals[SIGNAL_ACTIVITY_START_TRACKING] =
1200 g_signal_new ("activity-start-tracking", G_TYPE_FROM_CLASS (klass),
1201 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstLeaksTracerClass,
1202 activity_start_tracking), NULL, NULL, NULL, G_TYPE_NONE, 0,
1206 * GstLeaksTracer:::activity-get-checkpoint
1207 * @leakstracer: the leaks tracer object to emit this signal on
1209 * You must call this after calling `activity-start-tracking` and you should
1210 * call `activity-stop-tracking` when you are done tracking.
1212 * Returns a #GstStructure with two fields: `"objects-created-list"` and
1213 * `"objects-removed-list"`, each of which is a #GValue of type #GST_TYPE_LIST
1214 * containing all objects that were created/removed since the last
1215 * checkpoint, or since tracking started if this is the first checkpoint.
1217 * The list elements are in order of creation/removal. Each list element is
1218 * a #GValue containing a #GstStructure with the following fields:
1220 * `type-name`: a string representing the type of the object
1221 * `address`: a string representing the address of the object; the object
1222 * itself cannot be returned since we don't own it and it may be
1223 * freed at any moment, or it may already have been freed
1225 * Returns: (transfer full): a newly-allocated #GstStructure
1229 gst_leaks_tracer_signals[SIGNAL_ACTIVITY_GET_CHECKPOINT] =
1230 g_signal_new ("activity-get-checkpoint", G_TYPE_FROM_CLASS (klass),
1231 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstLeaksTracerClass,
1232 activity_get_checkpoint), NULL, NULL, NULL, GST_TYPE_STRUCTURE, 0,
1236 * GstLeaksTracer:::activity-log-checkpoint
1237 * @leakstracer: the leaks tracer object to emit this signal on
1239 * You must call this after calling `activity-start-tracking` and you should
1240 * call `activity-stop-tracking` when you are done tracking.
1242 * List all objects that were created or removed since the last checkpoint,
1243 * or since tracking started if this is the first checkpoint.
1245 * This action signal is equivalent to `activity-get-checkpoint` except that
1246 * the checkpoint data will be printed to the debug log under `GST_TRACER:7`.
1250 gst_leaks_tracer_signals[SIGNAL_ACTIVITY_LOG_CHECKPOINT] =
1251 g_signal_new ("activity-log-checkpoint", G_TYPE_FROM_CLASS (klass),
1252 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstLeaksTracerClass,
1253 activity_log_checkpoint), NULL, NULL, NULL, G_TYPE_NONE, 0,
1257 * GstLeaksTracer:::activity-stop-tracking
1258 * @leakstracer: the leaks tracer object to emit this signal on
1260 * Stop tracking all objects that are being created or removed, undoes the
1261 * effects of the `start-tracking` signal.
1265 gst_leaks_tracer_signals[SIGNAL_ACTIVITY_STOP_TRACKING] =
1266 g_signal_new ("activity-stop-tracking", G_TYPE_FROM_CLASS (klass),
1267 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstLeaksTracerClass,
1268 activity_stop_tracking), NULL, NULL, NULL, G_TYPE_NONE, 0,
1271 klass->get_live_objects = gst_leaks_tracer_get_live_objects;
1272 klass->log_live_objects = gst_leaks_tracer_log_live_objects;
1273 klass->activity_start_tracking = gst_leaks_tracer_activity_start_tracking;
1274 klass->activity_get_checkpoint = gst_leaks_tracer_activity_get_checkpoint;
1275 klass->activity_log_checkpoint = gst_leaks_tracer_activity_log_checkpoint;
1276 klass->activity_stop_tracking = gst_leaks_tracer_activity_stop_tracking;