1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
/*
* $Id: gwydebugobjects.c 21692 2018-11-26 13:29:49Z yeti-dn $
* Copyright (C) 2003-2004 David Necas (Yeti), Petr Klapetek.
* E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <libgwyddion/gwyutils.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwydebugobjects.h>
typedef struct {
gulong id;
gdouble create_time;
gdouble destroy_time;
GType type;
gpointer address;
const gchar *details;
} DebugObjectInfo;
static gboolean debug_objects_enabled = FALSE; /* Threads: debug only */
static GTimer *debug_objects_timer = NULL; /* Threads: debug only */
static GList *debug_objects = NULL; /* Threads: debug only */
static gsize id = 0; /* Threads: debug only */
/**
* gwy_debug_objects_enable:
* @enable: Whether object creation/destruction debugger should be enabled.
*
* Enables or disables the object creation/destruction debugger.
*
* When debugger is disabled, no new objects are noted, but destruction of
* already watched ones is still noted.
**/
void
gwy_debug_objects_enable(gboolean enable)
{
debug_objects_enabled = enable;
}
static void
debug_objects_set_time(gpointer data, G_GNUC_UNUSED GObject *exobject)
{
gdouble *elapsed_time = (gdouble*)data;
*elapsed_time = g_timer_elapsed(debug_objects_timer, NULL);
}
/**
* gwy_debug_objects_creation_detailed:
* @object: An object to watch.
* @details: Detailed information printed in debug output. No copy is made,
* it must exist permanently.
*
* Notes down @object and sets up watch for its destruction.
*
* This function should be called on object creation to get accurate creation
* time, but can be in fact called anytime in object existence.
*
* There are two possible uses: In class implementation, where it should be
* put into instance init function. Constructors are less suited for that,
* as there can be more than one, there can be deserializators, duplicators,
* etc., and you usually want to catch all possible means of object creation.
*
* Or it can be used on the side of object user who is concerned with lifetime
* rules of a particular object, he then calls it just after object creation.
**/
void
gwy_debug_objects_creation_detailed(GObject *object,
const gchar *details)
{
DebugObjectInfo *info;
if (!G_UNLIKELY(debug_objects_enabled))
return;
if (!id) {
g_assert(!debug_objects_timer && !debug_objects);
debug_objects_timer = g_timer_new();
}
info = g_slice_new(DebugObjectInfo);
info->id = ++id;
info->type = G_TYPE_FROM_INSTANCE(object);
info->address = object;
info->details = details;
info->create_time = g_timer_elapsed(debug_objects_timer, NULL);
info->destroy_time = -1;
g_object_weak_ref(info->address, &debug_objects_set_time,
&info->destroy_time);
debug_objects = g_list_prepend(debug_objects, info);
gwy_debug("Added watch for %s %p",
g_type_name(info->type), info->address);
}
/**
* gwy_debug_objects_dump_to_file:
* @filehandle: A filehandle open for writing.
* @flags: Dump option flags.
*
* Dumps all recorded objects to a file.
*
* The format of each line is: object type name, object address, creation time,
* destruction time (or ALIVE! message with reference count).
**/
void
gwy_debug_objects_dump_to_file(FILE *filehandle,
GwyDebugObjectsDumpFlags flags)
{
GList *l;
DebugObjectInfo *info;
for (l = g_list_last(debug_objects); l; l = g_list_previous(l)) {
info = (DebugObjectInfo*)l->data;
if ((flags & GWY_DEBUG_OBJECTS_DUMP_ONLY_ALIVE)
&& info->destroy_time >= 0.0)
continue;
gwy_fprintf(filehandle, "%s %p ", g_type_name(info->type), info->address);
if (info->details)
gwy_fprintf(filehandle, "(%s) ", info->details);
gwy_fprintf(filehandle, "%.3f ", info->create_time);
if (info->destroy_time > 0)
gwy_fprintf(filehandle, "%.3f\n", info->destroy_time);
else
gwy_fprintf(filehandle, "ALIVE(%d)!\n",
G_OBJECT(info->address)->ref_count);
}
}
/**
* gwy_debug_objects_clear:
*
* Frees all memory taken by debugger, removes all watches.
*
* Eventual following call to gwy_debug_objects_creation() will behave like
* the very first one, including time counting reset.
**/
void
gwy_debug_objects_clear(void)
{
GList *l;
DebugObjectInfo *info;
if (!id)
return;
for (l = debug_objects; l; l = g_list_next(l)) {
info = (DebugObjectInfo*)l->data;
if (info->destroy_time < 0.0)
g_object_weak_unref(info->address, &debug_objects_set_time,
&info->destroy_time);
g_slice_free(DebugObjectInfo, info);
}
g_list_free(debug_objects);
g_timer_destroy(debug_objects_timer);
id = 0;
debug_objects = NULL;
debug_objects_timer = NULL;
}
/************************** Documentation ****************************/
/**
* SECTION:gwydebugobjects
* @title: gwydebugobjects
* @short_description: Helps chasing leaking objects (debug)
*
* If you wonder about some object lifetime rules, these functions can help
* you: gwy_debug_objects_creation() hooks object finalization so it is
* possible to tell later whether and when the object was destroyed -- use
* gwy_debug_objects_dump_to_file() to dump this information to a file.
*
* If debugging is not enabled with gwy_debug_objects_enable() these functions
* do nothing. Note all Gwyddion data-like objects (i.e., not widgets) already
* call gwy_debug_objects_creation() so to debug their lifetime rules, just
* enable it.
*
* Future changes: This functionality will be removed in 3.0. Use RefDbg.
**/
/**
* GwyDebugObjectsDumpFlags:
* @GWY_DEBUG_OBJECTS_DUMP_ONLY_ALIVE: Dump only objects that are still
* alive.
*
* Option flags for gwy_debug_objects_dump_to_file().
**/
/**
* gwy_debug_objects_creation:
* @o: An object to watch.
*
* Convenience wrapper for gwy_debug_objects_creation_detailed().
*
* It uses file name and line number as the detail.
**/
/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
|