summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2019-02-06 18:40:49 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2019-02-06 18:40:49 (GMT)
commit7b3ca7accd4103bb5069d31b2389ea76c57846ca (patch)
treecdc9e63e83691131bcb427148edd340bd353d791 /src
parent3e6da2a6085a2bc04dd5324f7ee74633a53543f3 (diff)
Introduced a tracker for GObjects leaks.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am7
-rw-r--r--src/gleak.c137
-rw-r--r--src/gleak.h34
-rw-r--r--src/main.c6
4 files changed, 184 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "gleak.h"
+
+
+#include <glib-object.h>
+#include <malloc.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+
+#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 <http://www.gnu.org/licenses/>.
+ */
+
+
+#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 <config.h>
#include <i18n.h>
+
+#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;
}