From 7b3ca7accd4103bb5069d31b2389ea76c57846ca Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Wed, 6 Feb 2019 19:40:49 +0100 Subject: Introduced a tracker for GObjects leaks. --- configure.ac | 16 +++++++ src/Makefile.am | 7 +++ src/gleak.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/gleak.h | 34 ++++++++++++++ src/main.c | 6 +++ 5 files changed, 200 insertions(+) create mode 100644 src/gleak.c create mode 100644 src/gleak.h diff --git a/configure.ac b/configure.ac index 3043e7e..11a6a85 100644 --- a/configure.ac +++ b/configure.ac @@ -162,6 +162,11 @@ fi AC_ARG_ENABLE(debug, AS_HELP_STRING([--enable-debug], [compile with debugging support [default=no]]), [], [enable_debug=no]) +AC_ARG_WITH(gobject-leak-tracker, + AS_HELP_STRING([--with-gobject-leak-tracker], + [track remaining GObject instances at exit (imply --enable-debug and GLib support) [default=no]]), + [], [with_gobject_leak_tracker=no]) + AC_ARG_WITH(local-resources, AS_HELP_STRING([--with-local-resources], [discard resources from local sources [default=yes]]), [], [with_local_resources=yes]) @@ -172,6 +177,10 @@ CFLAGS="$CFLAGS -fshort-enums -D_LARGEFILE64_SOURCE" #--- Is debug mode needed ? +if test "x$with_gobject_leak_tracker" = "xyes"; then + enable_debug="yes" +fi + if test "x$enable_debug" = "xyes"; then DEBUG_CFLAGS="$DEBUG_CFLAGS -O0 -ggdb -gdwarf-2 -DDEBUG" else @@ -180,6 +189,13 @@ fi AC_SUBST(DEBUG_CFLAGS) +AM_CONDITIONAL([TRACK_GOBJECT_LEAKS], [test "x$with_gobject_leak_tracker" = "xyes"]) + +if test "x$with_gobject_leak_tracker" = "xyes"; then + AC_DEFINE(TRACK_GOBJECT_LEAKS, 1, + [Define to 1 to enable code for dumping remaining GObject instances at exit.]) +fi + #--- Discard local sources when looking for resources ? if test "x$with_local_resources" = "xno"; then diff --git a/src/Makefile.am b/src/Makefile.am index 31e1d72..ffa4bb5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -35,7 +35,14 @@ libchrysacore_la_LIBADD = \ # Programme principal ############################################################ +if TRACK_GOBJECT_LEAKS + +GOBJECT_LEAKS_SOURCES = gleak.h gleak.c + +endif + chrysalide_SOURCES = \ + $(GOBJECT_LEAKS_SOURCES) \ main.c diff --git a/src/gleak.c b/src/gleak.c new file mode 100644 index 0000000..3212d9f --- /dev/null +++ b/src/gleak.c @@ -0,0 +1,137 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * gleak.c - aide à la détection de fuites d'instances de GTypes + * + * Copyright (C) 2019 Cyrille Bagard + * + * This file is part of Chrysalide. + * + * Chrysalide 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 3 of the License, or + * (at your option) any later version. + * + * Chrysalide 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 Chrysalide. If not, see . + */ + + +#include "gleak.h" + + +#include +#include +#include +#include +#include + + +#include "common/environment.h" + + + +#define DUMP_OPEN_MSG "\n---------- GType instance leak dump ----------\n\n" +#define DUMP_CLOSE_MSG "\n----------------------------------------------\n\n" + + + +/* Parcourt l'arborescence des types à la recherche de fuites. */ +static void track_gtype_for_leak(GType, bool *); + + + +/****************************************************************************** +* * +* Paramètres : root = racine des types d'instance à retrouver. * +* first = suivi des premières impressions. [OUT] * +* * +* Description : Parcourt l'arborescence des types à la recherche de fuites. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void track_gtype_for_leak(GType root, bool *first) +{ + GType *children; /* Liste de tous les sous-types*/ + guint count; /* Taille de cette liste */ + GType *iter; /* Boucle de parcours */ + int remaining; /* Nombre d'instances restantes*/ + + children = g_type_children(root, &count); + + if (count == 0) + { + remaining = g_type_get_instance_count(root); + + if (remaining > 0) + { + if (*first) + { + fprintf(stderr, DUMP_OPEN_MSG); + *first = false; + } + + fprintf(stderr, "%s: %d\n", g_type_name(root), remaining); + + } + + } + + else + for (iter = children; *iter != 0; iter++) + track_gtype_for_leak(*iter, first); + + g_free(children); + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Affiche la liste des instances courantes restantes par type. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void dump_remaining_gtypes(void) +{ + char *debug; /* Conditions d'environnement */ + bool first; /* Première fois ? */ + + debug = get_env_var("GOBJECT_DEBUG"); + + if (strstr(debug, "instance-count") == NULL) + { + fprintf(stderr, DUMP_OPEN_MSG); + fprintf(stderr, "The GOBJECT_DEBUG variable does not include instance-count.\n"); + fprintf(stderr, "Exiting the dumping process...\n"); + fprintf(stderr, DUMP_CLOSE_MSG); + } + + else + { + first = true; + + track_gtype_for_leak(G_TYPE_OBJECT, &first); + + if (!first) + fprintf(stderr, DUMP_CLOSE_MSG); + + } + + free(debug); + +} diff --git a/src/gleak.h b/src/gleak.h new file mode 100644 index 0000000..233fabb --- /dev/null +++ b/src/gleak.h @@ -0,0 +1,34 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * gleak.h - prototypes pour l'aide à la détection de fuites d'instances de GTypes + * + * Copyright (C) 2019 Cyrille Bagard + * + * This file is part of Chrysalide. + * + * Chrysalide 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 3 of the License, or + * (at your option) any later version. + * + * Chrysalide 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 Chrysalide. If not, see . + */ + + +#ifndef _GLEAK_H +#define _GLEAK_H + + + +/* Affiche la liste des instances courantes restantes par type. */ +void dump_remaining_gtypes(void); + + + +#endif /* _GLEAK_H */ diff --git a/src/main.c b/src/main.c index 9ae3936..ef0335a 100644 --- a/src/main.c +++ b/src/main.c @@ -32,6 +32,8 @@ #include #include + +#include "gleak.h" #include "analysis/binary.h" #include "analysis/loading.h" #include "analysis/contents/file.h" @@ -416,6 +418,10 @@ int main(int argc, char **argv) done: +#ifdef TRACK_GOBJECT_LEAKS + dump_remaining_gtypes(); +#endif + return result; } -- cgit v0.11.2-87-g4458