summaryrefslogtreecommitdiff
path: root/src/gleak.c
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/gleak.c
parent3e6da2a6085a2bc04dd5324f7ee74633a53543f3 (diff)
Introduced a tracker for GObjects leaks.
Diffstat (limited to 'src/gleak.c')
-rw-r--r--src/gleak.c137
1 files changed, 137 insertions, 0 deletions
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);
+
+}