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:element-leakstracer
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
object_refing_info_free(ObjectRefingInfo * refinfo)74 object_refing_info_free (ObjectRefingInfo * refinfo)
75 {
76 g_free (refinfo->trace);
77 g_free (refinfo);
78 }
79
80 static void
object_refing_infos_free(ObjectRefingInfos * infos)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
set_print_stack_trace_from_string(GstLeaksTracer * self,const gchar * str)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
set_print_stack_trace(GstLeaksTracer * self,GstStructure * params)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
set_filters(GstLeaksTracer * self,const gchar * filters)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
set_params_from_structure(GstLeaksTracer * self,GstStructure * params)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
set_params(GstLeaksTracer * self)169 set_params (GstLeaksTracer * self)
170 {
171 gchar *params, *tmp;
172 GstStructure *params_struct = NULL;
173
174 g_object_get (self, "params", ¶ms, 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
_expand_unhandled_filters(gchar * typename,gpointer unused_value,GstLeaksTracer * self)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
should_handle_object_type(GstLeaksTracer * self,GType object_type)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 *
object_log_new(gpointer obj)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
object_log_free(ObjectLog * obj)269 object_log_free (ObjectLog * obj)
270 {
271 g_slice_free (ObjectLog, obj);
272 }
273 #endif /* G_OS_UNIX */
274
275 static void
handle_object_destroyed(GstLeaksTracer * self,gpointer object)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
object_weak_cb(gpointer data,GObject * object)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
mini_object_weak_cb(gpointer data,GstMiniObject * object)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
handle_object_created(GstLeaksTracer * self,gpointer object,GType type,gboolean gobject)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
mini_object_created_cb(GstTracer * tracer,GstClockTime ts,GstMiniObject * object)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
object_created_cb(GstTracer * tracer,GstClockTime ts,GstObject * object)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
handle_object_reffed(GstLeaksTracer * self,gpointer object,gint new_refcount,gboolean reffed,GstClockTime ts)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
object_reffed_cb(GstTracer * tracer,GstClockTime ts,GstObject * object,gint new_refcount)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
object_unreffed_cb(GstTracer * tracer,GstClockTime ts,GstObject * object,gint new_refcount)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
mini_object_reffed_cb(GstTracer * tracer,GstClockTime ts,GstMiniObject * object,gint new_refcount)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
mini_object_unreffed_cb(GstTracer * tracer,GstClockTime ts,GstMiniObject * object,gint new_refcount)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
gst_leaks_tracer_init(GstLeaksTracer * self)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
gst_leaks_tracer_constructed(GObject * object)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 *
leak_new(gpointer obj,GType type,guint ref_count,ObjectRefingInfos * infos)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
leak_free(Leak * leak)493 leak_free (Leak * leak)
494 {
495 g_free (leak->desc);
496 g_slice_free (Leak, leak);
497 }
498
499 static gint
sort_leaks(gconstpointer _a,gconstpointer _b)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 *
create_leaks_list(GstLeaksTracer * self)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
log_leaked(GstLeaksTracer * self)545 log_leaked (GstLeaksTracer * self)
546 {
547 GList *ref, *leaks, *l;
548 gboolean ret = FALSE;
549
550 GST_TRACE_OBJECT (self, "start listing currently alive objects");
551
552 leaks = create_leaks_list (self);
553 if (!leaks) {
554 GST_TRACE_OBJECT (self, "No objects alive currently");
555 goto done;
556 }
557
558 for (l = leaks; l != NULL; l = g_list_next (l)) {
559 Leak *leak = l->data;
560
561 gst_tracer_record_log (tr_alive, leak->type_name, leak->obj, leak->desc,
562 leak->ref_count,
563 leak->infos->creation_trace ? leak->infos->creation_trace : "");
564
565 leak->infos->refing_infos = g_list_reverse (leak->infos->refing_infos);
566 for (ref = leak->infos->refing_infos; ref; ref = ref->next) {
567 ObjectRefingInfo *refinfo = (ObjectRefingInfo *) ref->data;
568
569 gst_tracer_record_log (tr_refings, refinfo->ts, leak->type_name,
570 leak->obj, refinfo->reffed ? "reffed" : "unreffed",
571 refinfo->new_refcount, refinfo->trace ? refinfo->trace : "");
572 }
573 }
574
575 g_list_free_full (leaks, (GDestroyNotify) leak_free);
576
577 ret = TRUE;
578
579 done:
580 GST_TRACE_OBJECT (self, "done listing currently alive objects");
581
582 return ret;
583 }
584
585 static void
gst_leaks_tracer_finalize(GObject * object)586 gst_leaks_tracer_finalize (GObject * object)
587 {
588 GstLeaksTracer *self = GST_LEAKS_TRACER (object);
589 gboolean leaks;
590 GHashTableIter iter;
591 gpointer obj;
592
593 self->done = TRUE;
594
595 /* Tracers are destroyed as part of gst_deinit() so now is a good time to
596 * report all the objects which are still alive. */
597 leaks = log_leaked (self);
598
599 /* Remove weak references */
600 g_hash_table_iter_init (&iter, self->objects);
601 while (g_hash_table_iter_next (&iter, &obj, NULL)) {
602 if (GST_IS_OBJECT (obj))
603 g_object_weak_unref (obj, object_weak_cb, self);
604 else
605 gst_mini_object_weak_unref (GST_MINI_OBJECT_CAST (obj),
606 mini_object_weak_cb, self);
607 }
608
609 g_clear_pointer (&self->objects, g_hash_table_unref);
610 if (self->filter)
611 g_array_free (self->filter, TRUE);
612 g_clear_pointer (&self->added, g_hash_table_unref);
613 g_clear_pointer (&self->removed, g_hash_table_unref);
614 g_clear_pointer (&self->unhandled_filter, g_hash_table_unref);
615
616 g_queue_remove (&instances, self);
617
618 if (leaks)
619 g_warning ("Leaks detected");
620
621 ((GObjectClass *) gst_leaks_tracer_parent_class)->finalize (object);
622 }
623
624 #define RECORD_FIELD_TYPE_TS \
625 "ts", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
626 "type", G_TYPE_GTYPE, GST_TYPE_CLOCK_TIME, \
627 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
628 NULL)
629 #define RECORD_FIELD_TYPE_NAME \
630 "type-name", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
631 "type", G_TYPE_GTYPE, G_TYPE_STRING, \
632 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
633 NULL)
634 #define RECORD_FIELD_ADDRESS \
635 "address", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
636 "type", G_TYPE_GTYPE, G_TYPE_POINTER, \
637 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, \
638 GST_TRACER_VALUE_SCOPE_PROCESS, \
639 NULL)
640 #define RECORD_FIELD_DESC \
641 "description", 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 #define RECORD_FIELD_REF_COUNT \
646 "ref-count", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
647 "type", G_TYPE_GTYPE, G_TYPE_UINT, \
648 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
649 NULL)
650 #define RECORD_FIELD_TRACE \
651 "trace", GST_TYPE_STRUCTURE, gst_structure_new ("value", \
652 "type", G_TYPE_GTYPE, G_TYPE_STRING, \
653 "related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS, \
654 NULL)
655
656 #ifdef G_OS_UNIX
657 static void
sig_usr1_handler_foreach(gpointer data,gpointer user_data)658 sig_usr1_handler_foreach (gpointer data, gpointer user_data)
659 {
660 GstLeaksTracer *tracer = data;
661
662 GST_OBJECT_LOCK (tracer);
663 log_leaked (tracer);
664 GST_OBJECT_UNLOCK (tracer);
665 }
666
667 static void
sig_usr1_handler(G_GNUC_UNUSED int signal)668 sig_usr1_handler (G_GNUC_UNUSED int signal)
669 {
670 g_queue_foreach (&instances, sig_usr1_handler_foreach, NULL);
671 }
672
673 static void
log_checkpoint(GHashTable * hash,GstTracerRecord * record)674 log_checkpoint (GHashTable * hash, GstTracerRecord * record)
675 {
676 GHashTableIter iter;
677 gpointer o;
678
679 g_hash_table_iter_init (&iter, hash);
680 while (g_hash_table_iter_next (&iter, &o, NULL)) {
681 ObjectLog *obj = o;
682
683 gst_tracer_record_log (record, obj->type_name, obj->object);
684 }
685 }
686
687 static void
do_checkpoint(GstLeaksTracer * self)688 do_checkpoint (GstLeaksTracer * self)
689 {
690 GST_TRACE_OBJECT (self, "listing objects created since last checkpoint");
691 log_checkpoint (self->added, tr_added);
692 GST_TRACE_OBJECT (self, "listing objects removed since last checkpoint");
693 log_checkpoint (self->removed, tr_removed);
694
695 g_hash_table_remove_all (self->added);
696 g_hash_table_remove_all (self->removed);
697 }
698
699 static void
sig_usr2_handler_foreach(gpointer data,gpointer user_data)700 sig_usr2_handler_foreach (gpointer data, gpointer user_data)
701 {
702 GstLeaksTracer *tracer = data;
703
704 GST_OBJECT_LOCK (tracer);
705
706 if (!tracer->added) {
707 GST_TRACE_OBJECT (tracer, "First checkpoint, start tracking objects");
708
709 tracer->added = g_hash_table_new_full (NULL, NULL,
710 (GDestroyNotify) object_log_free, NULL);
711 tracer->removed = g_hash_table_new_full (NULL, NULL,
712 (GDestroyNotify) object_log_free, NULL);
713 } else {
714 do_checkpoint (tracer);
715 }
716
717 GST_OBJECT_UNLOCK (tracer);
718 }
719
720 static void
sig_usr2_handler(G_GNUC_UNUSED int signal)721 sig_usr2_handler (G_GNUC_UNUSED int signal)
722 {
723 g_queue_foreach (&instances, sig_usr2_handler_foreach, NULL);
724 }
725
726 static void
setup_signals(void)727 setup_signals (void)
728 {
729 tr_added = gst_tracer_record_new ("object-added.class",
730 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, NULL);
731 GST_OBJECT_FLAG_SET (tr_added, GST_OBJECT_FLAG_MAY_BE_LEAKED);
732
733 tr_removed = gst_tracer_record_new ("object-removed.class",
734 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, NULL);
735 GST_OBJECT_FLAG_SET (tr_removed, GST_OBJECT_FLAG_MAY_BE_LEAKED);
736
737 signal (SIGUSR1, sig_usr1_handler);
738 signal (SIGUSR2, sig_usr2_handler);
739 }
740 #endif /* G_OS_UNIX */
741
742 static void
gst_leaks_tracer_class_init(GstLeaksTracerClass * klass)743 gst_leaks_tracer_class_init (GstLeaksTracerClass * klass)
744 {
745 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
746
747 gobject_class->constructed = gst_leaks_tracer_constructed;
748 gobject_class->finalize = gst_leaks_tracer_finalize;
749
750 tr_alive = gst_tracer_record_new ("object-alive.class",
751 RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS, RECORD_FIELD_DESC,
752 RECORD_FIELD_REF_COUNT, RECORD_FIELD_TRACE, NULL);
753 GST_OBJECT_FLAG_SET (tr_alive, GST_OBJECT_FLAG_MAY_BE_LEAKED);
754
755 tr_refings = gst_tracer_record_new ("object-refings.class",
756 RECORD_FIELD_TYPE_TS, RECORD_FIELD_TYPE_NAME, RECORD_FIELD_ADDRESS,
757 RECORD_FIELD_DESC, RECORD_FIELD_REF_COUNT, RECORD_FIELD_TRACE, NULL);
758 GST_OBJECT_FLAG_SET (tr_alive, GST_OBJECT_FLAG_MAY_BE_LEAKED);
759
760 if (g_getenv ("GST_LEAKS_TRACER_SIG")) {
761 #ifdef G_OS_UNIX
762 setup_signals ();
763 #else
764 g_warning ("System doesn't support POSIX signals");
765 #endif /* G_OS_UNIX */
766 }
767 }
768