summaryrefslogtreecommitdiff
path: root/plugins/devdbg/speed.c
blob: 6b9cc6b74eaf349c496558ea359beb7dfb8b7c29 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

/* Chrysalide - Outil d'analyse de fichiers binaires
 * speed.c - mesure de temps d'exécution internes
 *
 * Copyright (C) 2015-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 "speed.h"


#include <assert.h>
#include <malloc.h>
#include <string.h>
#include <sys/time.h>


#include <i18n.h>


#include <common/extstr.h>
#include <plugins/plugin-def.h>



DEFINE_CHRYSALIDE_PLUGIN("GSpeedPlugin", "Speed Measure", "Tracks to time spent for disassembling code", "0.1.0",
                         NO_REQ, AL(PGA_FORMAT_ANALYSIS_STARTED,PGA_FORMAT_ANALYSIS_ENDED,
                                    PGA_FORMAT_POST_ANALYSIS_STARTED, PGA_FORMAT_POST_ANALYSIS_ENDED,
                                    PGA_DISASSEMBLY_STARTED, PGA_DISASSEMBLY_ENDED));


/* Mémorisation des résultats de chronométrages */
typedef struct _speed_measure
{
    unsigned long usages[2];                /* Taux d'utilisation du CPU   */

} speed_measure;


/* Affiche une mesure de temps écoulé. */
static void show_elapsed_time(const GPluginModule *, const char *, const speed_measure *);



/******************************************************************************
*                                                                             *
*  Paramètres  : plugin  = greffon à manipuler.                               *
*                title   = désignation humaine de la mesure menée.            *
*                measure = mesure de temps écoulé.                            *
*                                                                             *
*  Description : Affiche une mesure de temps écoulé.                          *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void show_elapsed_time(const GPluginModule *plugin, const char *title, const speed_measure *measure)
{
    char *tmp;                              /* Construction temporaire     */
    double seconds;                         /* Secondes écoulées           */
    unsigned long minutes;                  /* Minutes écoulées            */
    unsigned long hours;                    /* Heures écoulées             */
    char *msg;                              /* Message à faire passer      */

    tmp = NULL;

    seconds = (double)(measure->usages[1] - measure->usages[0]) / 1000000;

    if (seconds > 60)
    {
        minutes = seconds / 60;
        seconds -= minutes * 60;
    }
    else
        minutes = 0;

    if (minutes > 60)
    {
        hours = minutes / 60;
        minutes %= 60;
    }
    else
        hours = 0;

    msg = strdup(title);

    if (hours > 0)
    {
        asprintf(&tmp, " %lu", hours);
        msg = stradd(msg, tmp);
        free(tmp);

        msg = stradd(msg, _("h"));

    }

    if (minutes > 0)
    {
        asprintf(&tmp, " %lu", minutes);
        msg = stradd(msg, tmp);
        free(tmp);

        msg = stradd(msg, _("m"));

    }

    if (seconds > 0.01)
    {
        asprintf(&tmp, " %.2f", seconds);
        msg = stradd(msg, tmp);
        free(tmp);

        msg = stradd(msg, _("s"));

    }

    if (tmp != NULL)
        g_plugin_module_log_simple_message(plugin, LMT_INFO, msg);

    free(msg);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à manipuler.                                *
*                action = type d'action attendue.                             *
*                format = format de binaire à manipuler pendant l'opération.  *
*                gid    = groupe de travail dédié.                            *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Procède à une opération liée à l'analyse d'un format.        *
*                                                                             *
*  Retour      : Bilan de l'exécution du traitement.                          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

G_MODULE_EXPORT bool handle_binary_format_analysis(const GPluginModule *plugin, PluginAction action, GBinFormat *format, wgroup_id_t gid, GtkStatusStack *status)
{
    speed_measure *measure;                 /* Suivi des progressions      */
    struct timeval point;                   /* Point de mesure courant     */

    switch (action)
    {
        case PGA_FORMAT_ANALYSIS_STARTED:
        case PGA_FORMAT_POST_ANALYSIS_STARTED:

            measure = (speed_measure *)calloc(1, sizeof(speed_measure));
            g_object_set_data(G_OBJECT(format), "speed_measure", measure);

            gettimeofday(&point, NULL);
            measure->usages[0] = point.tv_sec * 1000000 + point.tv_usec;

            break;

        case PGA_FORMAT_ANALYSIS_ENDED:
        case PGA_FORMAT_POST_ANALYSIS_ENDED:

            measure = (speed_measure *)g_object_get_data(G_OBJECT(format), "speed_measure");

            gettimeofday(&point, NULL);
            measure->usages[1] = point.tv_sec * 1000000 + point.tv_usec;

            if (action == PGA_FORMAT_ANALYSIS_ENDED)
                show_elapsed_time(plugin, _("Whole elapsed time for format analysis:"), measure);
            else
                show_elapsed_time(plugin, _("Whole elapsed time for format post-analysis:"), measure);

            g_object_set_data(G_OBJECT(format), "speed_measure", NULL);
            free(measure);

            break;

        default:
            assert(false);
            break;

    }

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin  = greffon à manipuler.                               *
*                action  = type d'action attendue.                            *
*                binary  = binaire dont le contenu est en cours de traitement.*
*                status  = barre de statut à tenir informée.                  *
*                context = contexte de désassemblage.                         *
*                                                                             *
*  Description : Exécute une action pendant un désassemblage de binaire.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

G_MODULE_EXPORT void process_binary_disassembly(const GPluginModule *plugin, PluginAction action, GLoadedBinary *binary, GtkStatusStack *status, GProcContext *context)
{
    speed_measure *measure;                 /* Suivi des progressions      */
    struct timeval point;                   /* Point de mesure courant     */

    switch (action)
    {
        case PGA_DISASSEMBLY_STARTED:

            measure = (speed_measure *)calloc(1, sizeof(speed_measure));
            g_object_set_data(G_OBJECT(binary), "speed_measure", measure);

            gettimeofday(&point, NULL);
            measure->usages[0] = point.tv_sec * 1000000 + point.tv_usec;

            break;

        case PGA_DISASSEMBLY_ENDED:

            measure = (speed_measure *)g_object_get_data(G_OBJECT(binary), "speed_measure");

            gettimeofday(&point, NULL);
            measure->usages[1] = point.tv_sec * 1000000 + point.tv_usec;

            show_elapsed_time(plugin, _("Whole elapsed time for disassembly:"), measure);

            g_object_set_data(G_OBJECT(binary), "speed_measure", NULL);
            free(measure);

            break;

        default:
            assert(false);
            break;

    }

}