leaks: Handle subclasses in filters even for unhandled/lazy loaded types
[platform/upstream/gstreamer.git] / plugins / tracers / gstleaks.c
1 /* GStreamer
2  * Copyright (C) 2016 Collabora Ltd. <guillaume.desmottes@collabora.co.uk>
3  *
4  * gstleaks.c: tracing module detecting object leaks
5  *
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.
10  *
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.
15  *
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.
20  */
21 /**
22  * SECTION:gstleaks
23  * @short_description: detect GstObject and GstMiniObject leaks
24  *
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)
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #  include "config.h"
33 #endif
34
35 #include "gstleaks.h"
36
37 #ifdef G_OS_UNIX
38 #include <signal.h>
39 #endif /* G_OS_UNIX */
40
41 GST_DEBUG_CATEGORY_STATIC (gst_leaks_debug);
42 #define GST_CAT_DEFAULT gst_leaks_debug
43
44 #define _do_init \
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);
49
50 static GstTracerRecord *tr_alive;
51 static GstTracerRecord *tr_refings;
52 #ifdef G_OS_UNIX
53 static GstTracerRecord *tr_added = NULL;
54 static GstTracerRecord *tr_removed = NULL;
55 #endif /* G_OS_UNIX */
56 static GQueue instances = G_QUEUE_INIT;
57
58 typedef struct
59 {
60   gboolean reffed;
61   gchar *trace;
62   gint new_refcount;
63   GstClockTime ts;
64 } ObjectRefingInfo;
65
66 typedef struct
67 {
68   gchar *creation_trace;
69
70   GList *refing_infos;
71 } ObjectRefingInfos;
72
73 static void
74 object_refing_info_free (ObjectRefingInfo * refinfo)
75 {
76   g_free (refinfo->trace);
77   g_free (refinfo);
78 }
79
80 static void
81 object_refing_infos_free (ObjectRefingInfos * infos)
82 {
83   g_list_free_full (infos->refing_infos,
84       (GDestroyNotify) object_refing_info_free);
85
86   g_free (infos->creation_trace);
87   g_free (infos);
88 }
89
90 static void
91 set_print_stack_trace_from_string (GstLeaksTracer * self, const gchar * str)
92 {
93   gchar *trace;
94
95   /* Test if we can retrieve backtrace */
96   trace = gst_debug_get_stack_trace (FALSE);
97   if (!trace)
98     return;
99
100   g_free (trace);
101
102   if (g_strcmp0 (str, "full") == 0)
103     self->trace_flags = GST_STACK_TRACE_SHOW_FULL;
104   else
105     self->trace_flags = 0;
106 }
107
108 static void
109 set_print_stack_trace (GstLeaksTracer * self, GstStructure * params)
110 {
111   const gchar *trace_flags = g_getenv ("GST_LEAKS_TRACER_STACK_TRACE");
112
113   self->trace_flags = -1;
114   if (!trace_flags && params)
115     trace_flags = gst_structure_get_string (params, "stack-traces-flags");
116
117   if (!trace_flags)
118     return;
119
120   set_print_stack_trace_from_string (self, trace_flags);
121 }
122
123 static void
124 set_filters (GstLeaksTracer * self, const gchar * filters)
125 {
126   guint i;
127   GStrv tmp = g_strsplit (filters, ",", -1);
128
129   self->filter = g_array_sized_new (FALSE, FALSE, sizeof (GType),
130       g_strv_length (tmp));
131   for (i = 0; tmp[i]; i++) {
132     GType type;
133
134     type = g_type_from_name (tmp[i]);
135     if (type == 0) {
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
140        * used. */
141       if (!self->unhandled_filter)
142         self->unhandled_filter = g_hash_table_new_full (g_str_hash, g_str_equal,
143             g_free, NULL);
144
145       g_hash_table_add (self->unhandled_filter, g_strdup (tmp[i]));
146       g_atomic_int_inc (&self->unhandled_filter_count);
147       continue;
148     }
149
150     GST_DEBUG_OBJECT (self, "add filter on %s", tmp[i]);
151
152     g_array_append_val (self->filter, type);
153   }
154
155   g_strfreev (tmp);
156 }
157
158 static void
159 set_params_from_structure (GstLeaksTracer * self, GstStructure * params)
160 {
161   const gchar *filters = gst_structure_get_string (params, "filters");
162
163   if (filters)
164     set_filters (self, filters);
165   gst_structure_get_boolean (params, "check-refs", &self->check_refs);
166 }
167
168 static void
169 set_params (GstLeaksTracer * self)
170 {
171   gchar *params, *tmp;
172   GstStructure *params_struct = NULL;
173
174   g_object_get (self, "params", &params, NULL);
175   if (!params)
176     goto set_stacktrace;
177
178   tmp = g_strdup_printf ("leaks,%s", params);
179   params_struct = gst_structure_from_string (tmp, NULL);
180   g_free (tmp);
181
182   if (params_struct)
183     set_params_from_structure (self, params_struct);
184   else
185     set_filters (self, params);
186
187   g_free (params);
188
189 set_stacktrace:
190   set_print_stack_trace (self, params_struct);
191
192   if (params_struct)
193     gst_structure_free (params_struct);
194 }
195
196 static gboolean
197 _expand_unhandled_filters (gchar * typename, gpointer unused_value,
198     GstLeaksTracer * self)
199 {
200   GType type;
201
202   type = g_type_from_name (typename);
203
204   if (type == 0)
205     return FALSE;
206
207   g_atomic_int_dec_and_test (&self->unhandled_filter_count);
208   g_array_append_val (self->filter, type);
209
210   return TRUE;
211 }
212
213 static gboolean
214 should_handle_object_type (GstLeaksTracer * self, GType object_type)
215 {
216   guint i, len;
217
218   if (!self->filter)
219     /* No filtering, handle all types */
220     return TRUE;
221
222   if (object_type == 0)
223     return FALSE;
224
225
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);
231   }
232
233   len = self->filter->len;
234   for (i = 0; i < len; i++) {
235     GType type = g_array_index (self->filter, GType, i);
236
237     if (g_type_is_a (object_type, type))
238       return TRUE;
239   }
240
241   return FALSE;
242 }
243
244 #ifdef G_OS_UNIX
245 /* The object may be destroyed when we log it using the checkpointing system so
246  * we have to save its type name */
247 typedef struct
248 {
249   gpointer object;
250   const gchar *type_name;
251 } ObjectLog;
252
253 static ObjectLog *
254 object_log_new (gpointer obj)
255 {
256   ObjectLog *o = g_slice_new (ObjectLog);
257
258   o->object = obj;
259
260   if (G_IS_OBJECT (obj))
261     o->type_name = G_OBJECT_TYPE_NAME (obj);
262   else
263     o->type_name = g_type_name (GST_MINI_OBJECT_TYPE (obj));
264
265   return o;
266 }
267
268 static void
269 object_log_free (ObjectLog * obj)
270 {
271   g_slice_free (ObjectLog, obj);
272 }
273 #endif /* G_OS_UNIX */
274
275 static void
276 handle_object_destroyed (GstLeaksTracer * self, gpointer object)
277 {
278   GST_OBJECT_LOCK (self);
279   if (self->done) {
280     g_warning
281         ("object %p destroyed while the leaks tracer was finalizing. Some threads are still running?",
282         object);
283     goto out;
284   }
285
286   g_hash_table_remove (self->objects, object);
287 #ifdef G_OS_UNIX
288   if (self->removed)
289     g_hash_table_add (self->removed, object_log_new (object));
290 #endif /* G_OS_UNIX */
291 out:
292   GST_OBJECT_UNLOCK (self);
293 }
294
295 static void
296 object_weak_cb (gpointer data, GObject * object)
297 {
298   GstLeaksTracer *self = data;
299
300   handle_object_destroyed (self, object);
301 }
302
303 static void
304 mini_object_weak_cb (gpointer data, GstMiniObject * object)
305 {
306   GstLeaksTracer *self = data;
307
308   handle_object_destroyed (self, object);
309 }
310
311 static void
312 handle_object_created (GstLeaksTracer * self, gpointer object, GType type,
313     gboolean gobject)
314 {
315   ObjectRefingInfos *infos;
316
317
318   if (!should_handle_object_type (self, type))
319     return;
320
321   infos = g_malloc0 (sizeof (ObjectRefingInfos));
322   if (gobject)
323     g_object_weak_ref ((GObject *) object, object_weak_cb, self);
324   else
325     gst_mini_object_weak_ref (GST_MINI_OBJECT_CAST (object),
326         mini_object_weak_cb, self);
327
328   GST_OBJECT_LOCK (self);
329   if ((gint) self->trace_flags != -1)
330     infos->creation_trace = gst_debug_get_stack_trace (self->trace_flags);
331
332   g_hash_table_insert (self->objects, object, infos);
333
334 #ifdef G_OS_UNIX
335   if (self->added)
336     g_hash_table_add (self->added, object_log_new (object));
337 #endif /* G_OS_UNIX */
338   GST_OBJECT_UNLOCK (self);
339 }
340
341 static void
342 mini_object_created_cb (GstTracer * tracer, GstClockTime ts,
343     GstMiniObject * object)
344 {
345   GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
346
347   handle_object_created (self, object, GST_MINI_OBJECT_TYPE (object), FALSE);
348 }
349
350 static void
351 object_created_cb (GstTracer * tracer, GstClockTime ts, GstObject * object)
352 {
353   GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
354   GType object_type = G_OBJECT_TYPE (object);
355
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))
358     return;
359
360   handle_object_created (self, object, object_type, TRUE);
361 }
362
363 static void
364 handle_object_reffed (GstLeaksTracer * self, gpointer object, gint new_refcount,
365     gboolean reffed, GstClockTime ts)
366 {
367   ObjectRefingInfos *infos;
368   ObjectRefingInfo *refinfo;
369
370   if (!self->check_refs)
371     return;
372
373   GST_OBJECT_LOCK (self);
374   infos = g_hash_table_lookup (self->objects, object);
375   if (!infos)
376     goto out;
377
378   refinfo = g_malloc0 (sizeof (ObjectRefingInfo));
379   refinfo->ts = ts;
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);
384
385   infos->refing_infos = g_list_prepend (infos->refing_infos, refinfo);
386
387 out:
388   GST_OBJECT_UNLOCK (self);
389 }
390
391 static void
392 object_reffed_cb (GstTracer * tracer, GstClockTime ts, GstObject * object,
393     gint new_refcount)
394 {
395   GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
396
397   handle_object_reffed (self, object, new_refcount, TRUE, ts);
398 }
399
400 static void
401 object_unreffed_cb (GstTracer * tracer, GstClockTime ts, GstObject * object,
402     gint new_refcount)
403 {
404   GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
405
406   handle_object_reffed (self, object, new_refcount, FALSE, ts);
407 }
408
409 static void
410 mini_object_reffed_cb (GstTracer * tracer, GstClockTime ts,
411     GstMiniObject * object, gint new_refcount)
412 {
413   GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
414
415   handle_object_reffed (self, object, new_refcount, TRUE, ts);
416 }
417
418 static void
419 mini_object_unreffed_cb (GstTracer * tracer, GstClockTime ts,
420     GstMiniObject * object, gint new_refcount)
421 {
422   GstLeaksTracer *self = GST_LEAKS_TRACER_CAST (tracer);
423
424   handle_object_reffed (self, object, new_refcount, FALSE, ts);
425 }
426
427 static void
428 gst_leaks_tracer_init (GstLeaksTracer * self)
429 {
430   self->objects = g_hash_table_new_full (NULL, NULL, NULL,
431       (GDestroyNotify) object_refing_infos_free);
432
433   g_queue_push_tail (&instances, self);
434 }
435
436 static void
437 gst_leaks_tracer_constructed (GObject * object)
438 {
439   GstLeaksTracer *self = GST_LEAKS_TRACER (object);
440   GstTracer *tracer = GST_TRACER (object);
441
442   set_params (self);
443
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));
448
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));
458   }
459
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. */
463
464   ((GObjectClass *) gst_leaks_tracer_parent_class)->constructed (object);
465 }
466
467 typedef struct
468 {
469   gpointer obj;
470   const gchar *type_name;
471   guint ref_count;
472   gchar *desc;
473   ObjectRefingInfos *infos;
474 } Leak;
475
476 /* The content of the returned Leak struct is valid until the self->objects
477  * hash table has been modified. */
478 static Leak *
479 leak_new (gpointer obj, GType type, guint ref_count, ObjectRefingInfos * infos)
480 {
481   Leak *leak = g_slice_new (Leak);
482
483   leak->obj = obj;
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);
487   leak->infos = infos;
488
489   return leak;
490 }
491
492 static void
493 leak_free (Leak * leak)
494 {
495   g_free (leak->desc);
496   g_slice_free (Leak, leak);
497 }
498
499 static gint
500 sort_leaks (gconstpointer _a, gconstpointer _b)
501 {
502   const Leak *a = _a, *b = _b;
503
504   return g_strcmp0 (a->type_name, b->type_name);
505 }
506
507 static GList *
508 create_leaks_list (GstLeaksTracer * self)
509 {
510   GList *l = NULL;
511   GHashTableIter iter;
512   gpointer obj, infos;
513
514   g_hash_table_iter_init (&iter, self->objects);
515   while (g_hash_table_iter_next (&iter, &obj, &infos)) {
516     GType type;
517     guint ref_count;
518
519     if (GST_IS_OBJECT (obj)) {
520       if (GST_OBJECT_FLAG_IS_SET (obj, GST_OBJECT_FLAG_MAY_BE_LEAKED))
521         continue;
522
523       type = G_OBJECT_TYPE (obj);
524       ref_count = ((GObject *) obj)->ref_count;
525     } else {
526       if (GST_MINI_OBJECT_FLAG_IS_SET (obj, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED))
527         continue;
528
529       type = GST_MINI_OBJECT_TYPE (obj);
530       ref_count = ((GstMiniObject *) obj)->refcount;
531     }
532
533     l = g_list_prepend (l, leak_new (obj, type, ref_count, infos));
534   }
535
536   /* Sort leaks by type name so they are grouped together making the output
537    * easier to read */
538   l = g_list_sort (l, sort_leaks);
539
540   return l;
541 }
542
543 /* Return TRUE if at least one leaked object has been logged */
544 static gboolean
545 log_leaked (GstLeaksTracer * self)
546 {
547   GList *ref, *leaks, *l;
548
549   leaks = create_leaks_list (self);
550   if (!leaks)
551     return FALSE;
552
553   for (l = leaks; l != NULL; l = g_list_next (l)) {
554     Leak *leak = l->data;
555
556     gst_tracer_record_log (tr_alive, leak->type_name, leak->obj, leak->desc,
557         leak->ref_count,
558         leak->infos->creation_trace ? leak->infos->creation_trace : "");
559
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;
563
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 : "");
567     }
568   }
569
570   g_list_free_full (leaks, (GDestroyNotify) leak_free);
571
572   return TRUE;
573 }
574
575 static void
576 gst_leaks_tracer_finalize (GObject * object)
577 {
578   GstLeaksTracer *self = GST_LEAKS_TRACER (object);
579   gboolean leaks;
580   GHashTableIter iter;
581   gpointer obj;
582
583   self->done = TRUE;
584
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);
588
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);
594     else
595       gst_mini_object_weak_unref (GST_MINI_OBJECT_CAST (obj),
596           mini_object_weak_cb, self);
597   }
598
599   g_clear_pointer (&self->objects, g_hash_table_unref);
600   if (self->filter)
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);
605
606   g_queue_remove (&instances, self);
607
608   if (leaks)
609     g_warning ("Leaks detected");
610
611   ((GObjectClass *) gst_leaks_tracer_parent_class)->finalize (object);
612 }
613
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, \
618         NULL)
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, \
623         NULL)
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, \
629         NULL)
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, \
634         NULL)
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, \
639         NULL)
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, \
644         NULL)
645
646 #ifdef G_OS_UNIX
647 static void
648 sig_usr1_handler_foreach (gpointer data, gpointer user_data)
649 {
650   GstLeaksTracer *tracer = data;
651
652   GST_OBJECT_LOCK (tracer);
653   GST_TRACE_OBJECT (tracer, "start listing currently alive objects");
654   log_leaked (tracer);
655   GST_TRACE_OBJECT (tracer, "done listing currently alive objects");
656   GST_OBJECT_UNLOCK (tracer);
657 }
658
659 static void
660 sig_usr1_handler (G_GNUC_UNUSED int signal)
661 {
662   g_queue_foreach (&instances, sig_usr1_handler_foreach, NULL);
663 }
664
665 static void
666 log_checkpoint (GHashTable * hash, GstTracerRecord * record)
667 {
668   GHashTableIter iter;
669   gpointer o;
670
671   g_hash_table_iter_init (&iter, hash);
672   while (g_hash_table_iter_next (&iter, &o, NULL)) {
673     ObjectLog *obj = o;
674
675     gst_tracer_record_log (record, obj->type_name, obj->object);
676   }
677 }
678
679 static void
680 do_checkpoint (GstLeaksTracer * self)
681 {
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);
686
687   g_hash_table_remove_all (self->added);
688   g_hash_table_remove_all (self->removed);
689 }
690
691 static void
692 sig_usr2_handler_foreach (gpointer data, gpointer user_data)
693 {
694   GstLeaksTracer *tracer = data;
695
696   GST_OBJECT_LOCK (tracer);
697
698   if (!tracer->added) {
699     GST_TRACE_OBJECT (tracer, "First checkpoint, start tracking objects");
700
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);
705   } else {
706     do_checkpoint (tracer);
707   }
708
709   GST_OBJECT_UNLOCK (tracer);
710 }
711
712 static void
713 sig_usr2_handler (G_GNUC_UNUSED int signal)
714 {
715   g_queue_foreach (&instances, sig_usr2_handler_foreach, NULL);
716 }
717
718 static void
719 setup_signals (void)
720 {
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);
724
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);
728
729   signal (SIGUSR1, sig_usr1_handler);
730   signal (SIGUSR2, sig_usr2_handler);
731 }
732 #endif /* G_OS_UNIX */
733
734 static void
735 gst_leaks_tracer_class_init (GstLeaksTracerClass * klass)
736 {
737   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
738
739   gobject_class->constructed = gst_leaks_tracer_constructed;
740   gobject_class->finalize = gst_leaks_tracer_finalize;
741
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);
746
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);
751
752   if (g_getenv ("GST_LEAKS_TRACER_SIG")) {
753 #ifdef G_OS_UNIX
754     setup_signals ();
755 #else
756     g_warning ("System doesn't support POSIX signals");
757 #endif /* G_OS_UNIX */
758   }
759 }