/* 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); }