summaryrefslogtreecommitdiff
path: root/src/gleak.c
blob: 3212d9f1b01c7789a85b5aae92fe3d98c0eda648 (plain)
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

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

}