gstreamer/gst/gsttrace.c
Wim Taymans 321ee10bd6 trace: rework alloc tracing
Remove trace, we use debug log for that
Make alloc trace simpler, removing some methods.
Activate alloc trace with a GST_TRACE=3 environment variable.
Dump leaked objects atexit.
Provide an offset in the object where the GType can be found so that more
verbose info can be given for objects.
Remove -T option from gst-launch because tracing is now triggered with the
environment variable.
2012-01-27 17:54:43 +01:00

210 lines
4.8 KiB
C

/* GStreamer
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
* 2000 Wim Taymans <wtay@chello.be>
*
* gsttrace.c: Tracing functions (deprecated)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/**
* SECTION:gsttrace
* @short_description: Tracing functionality
*
* Traces allows to track object allocation. They provide a instance counter per
* #GType. The counter is incremented for each object allocated and decremented
* it when it's freed.
*
* <example>
* <title>Tracing object instances</title>
* <programlisting>
* // trace un-freed object instances
* gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
* if (!gst_alloc_trace_available ()) {
* g_warning ("Trace not available (recompile with trace enabled).");
* }
* gst_alloc_trace_print_live ();
* // do something here
* gst_alloc_trace_print_live ();
* </programlisting>
* </example>
*
* Last reviewed on 2005-11-21 (0.9.5)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#if defined (_MSC_VER) && _MSC_VER >= 1400
# include <io.h>
#endif
#include "gst_private.h"
#include "gstinfo.h"
#include "gsttrace.h"
GMutex _gst_trace_mutex;
/* global flags */
static GstAllocTraceFlags _gst_trace_flags = GST_ALLOC_TRACE_NONE;
/* list of registered tracers */
static GList *_gst_alloc_tracers = NULL;
static void
_at_exit (void)
{
if (_gst_trace_flags)
_priv_gst_alloc_trace_dump ();
}
void
_priv_gst_alloc_trace_initialize (void)
{
const gchar *trace;
trace = g_getenv ("GST_TRACE");
if (trace != NULL) {
_gst_trace_flags = atoi (trace);
atexit (_at_exit);
}
g_mutex_init (&_gst_trace_mutex);
}
void
_priv_gst_alloc_trace_deinit (void)
{
g_mutex_clear (&_gst_trace_mutex);
}
/**
* _priv_gst_alloc_trace_register:
* @name: the name of the new alloc trace object.
* @offset: the offset in the object where a GType an be found. -1 when the
* object has no gtype.
*
* Register an get a handle to a GstAllocTrace object that
* can be used to trace memory allocations.
*
* Returns: A handle to a GstAllocTrace.
*/
GstAllocTrace *
_priv_gst_alloc_trace_register (const gchar * name, goffset offset)
{
GstAllocTrace *trace;
g_return_val_if_fail (name, NULL);
trace = g_slice_new (GstAllocTrace);
trace->name = g_strdup (name);
trace->live = 0;
trace->mem_live = NULL;
trace->flags = _gst_trace_flags;
trace->offset = offset;
_gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
return trace;
}
static gint
compare_func (GstAllocTrace * a, GstAllocTrace * b)
{
return strcmp (a->name, b->name);
}
static GList *
gst_alloc_trace_list_sorted (void)
{
GList *ret;
ret = g_list_sort (g_list_copy (_gst_alloc_tracers),
(GCompareFunc) compare_func);
return ret;
}
static void
gst_alloc_trace_print (const GstAllocTrace * trace)
{
GSList *mem_live;
g_return_if_fail (trace != NULL);
if (trace->flags & GST_ALLOC_TRACE_LIVE) {
g_print ("%-22.22s : %d\n", trace->name, trace->live);
} else {
g_print ("%-22.22s : (no live count)\n", trace->name);
}
if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
mem_live = trace->mem_live;
while (mem_live) {
gpointer data = mem_live->data;
const gchar *type_name;
if (G_IS_OBJECT (data)) {
type_name = G_OBJECT_TYPE_NAME (data);
} else if (trace->offset != -1) {
GType type;
type = G_STRUCT_MEMBER (GType, data, trace->offset);
type_name = g_type_name (type);
} else {
type_name = "";
}
g_print (" %-20.20s : %p\n", type_name, data);
mem_live = mem_live->next;
}
}
}
/**
* _priv_gst_alloc_trace_dump:
*
* Print the status of all registered alloc trace objects.
*/
void
_priv_gst_alloc_trace_dump (void)
{
GList *orig, *walk;
orig = walk = gst_alloc_trace_list_sorted ();
while (walk) {
GstAllocTrace *trace = (GstAllocTrace *) walk->data;
gst_alloc_trace_print (trace);
walk = g_list_next (walk);
}
g_list_free (orig);
}