summaryrefslogtreecommitdiff
path: root/src/debug/jdwp/packet.c
blob: d5e3dba762c11c80eeae7cfb8ec2e7f1046c699a (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422

/* Chrysalide - Outil d'analyse de fichiers binaires
 * packet.c - définition des paquets destiné au protocole JDWP
 *
 * Copyright (C) 2010-2017 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "packet.h"


#include "misc/header.h"
#include "../packet-int.h"
#include "../../arch/archbase.h"
#include "../../common/endianness.h"



/* Répresentation d'un paquet de débogage JDWP (instance) */
struct _GJdwpPacket
{
    GDebugPacket parent;                    /* A laisser en premier        */

    jdwp_header header;                     /* En-tête du paquet JDWP      */
    bin_t hblob[sizeof(jdwp_header)];       /* Contenu encodé en B.E.      */

    jdwp_payload payload;                   /* Charge utile du paquet      */
    bin_t pblob[sizeof(jdwp_payload)];      /* Contenu encodé en B.E.      */
    bool got;                               /* Précise le type de charge   */

};


/* Répresentation d'un paquet de débogage JDWP (classe) */
struct _GJdwpPacketClass
{
    GDebugPacketClass parent;               /* A laisser en premier        */

    jdwp_cmd_vm_id_sizes_reply sizes;       /* Réf. des tailles dynamiques */

};


/* Initialise la classe des paquets de débogage JDWP. */
static void g_jdwp_packet_class_init(GJdwpPacketClass *);

/* Initialise une instance de paquet de débogage JDWP. */
static void g_jdwp_packet_init(GJdwpPacket *);

/* Précise les zones mémoires correspondant au contenu. */
static void g_jdwp_packet_vectorize(GJdwpPacket *, struct iovec [UIO_MAXIOV], int *);



/* Indique le type défini pour un paquet de débogage JDWP. */
G_DEFINE_TYPE(GJdwpPacket, g_jdwp_packet, G_TYPE_DEBUG_PACKET);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des paquets de débogage JDWP.           *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_jdwp_packet_class_init(GJdwpPacketClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise une instance de paquet de débogage JDWP.          *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_jdwp_packet_init(GJdwpPacket *packet)
{
    GDebugPacket *dpkt;                     /* Version parente             */

    dpkt = G_DEBUG_PACKET(packet);

    dpkt->vectorize = (debug_vectorize_fc)g_jdwp_packet_vectorize;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = instance à utiliser comme intermédiaire.            *
*                sizes  = références pour la valeur des tailles dynamiques.   *
*                                                                             *
*  Description : Enregistre les différentes tailles dynamiques.               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_jdwp_packet_set_sizes(GJdwpPacket *packet, const jdwp_cmd_vm_id_sizes_reply *sizes)
{
    G_JDWP_PACKET_GET_CLASS(packet)->sizes = *sizes;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = instance à convertir.                               *
*                iov    = table de vecteurs. [OUT]                            *
*                iovcnt = quantité de champs renseignés. [OUT]                *
*                                                                             *
*  Description : Précise les zones mémoires correspondant au contenu.         *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_jdwp_packet_vectorize(GJdwpPacket *packet, struct iovec iov[UIO_MAXIOV], int *iovcnt)
{
    uint32_t length;                        /* Quantité de données         */
    bool empty;                             /* Présence d'une charge utile */

    read_u32(&length, packet->hblob, (off_t []) { 0 }, sizeof(jdwp_header), SRE_BIG);

    iov[0].iov_base = packet->hblob;
    iov[0].iov_len = sizeof(jdwp_header);

    empty = (length == sizeof(jdwp_header));

    if (!empty)
    {
        iov[1].iov_base = packet->pblob;
        iov[1].iov_len = length - sizeof(jdwp_header);
    }

    *iovcnt = (empty ? 1 : 2);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = instance à consulter.                               *
*                                                                             *
*  Description : Fournit l'adresse des données de l'en-tête d'un paquet JDWP. *
*                                                                             *
*  Retour      : Adresse des données de l'en-tête (à priori de requête).      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

jdwp_header *g_jdwp_packet_get_header(GJdwpPacket *packet)
{
    return &packet->header;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = instance à consulter.                               *
*                                                                             *
*  Description : Fournit l'adresse des données de l'en-tête d'un paquet JDWP. *
*                                                                             *
*  Retour      : Adresse des données de l'en-tête (à priori de requête).      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bin_t *g_jdwp_packet_get_hblob(GJdwpPacket *packet)
{
    return packet->hblob;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = instance à mettre à jour.                           *
*                                                                             *
*  Description : Recompose l'en-tête d'un paquet à partir de données brutes.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_jdwp_packet_parse_header(GJdwpPacket *packet)
{
    return get_jdwp_header(packet->hblob, &packet->header);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet  = paquet à mettre à jour.                            *
*                set     = jeu de commandes de la requête.                    *
*                command = commande proprement dite.                          *
*                                                                             *
*  Description : Définit l'en-tête du paquet pour une requête au format JDWP. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_jdwp_packet_set_request_header(GJdwpPacket *packet, uint8_t set, uint8_t command)
{
    set_jdwp_request_header(&packet->header, packet->hblob,
                            0, set, command);

    packet->got = false;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet  = paquet à mettre à jour.                            *
*                lastid = jeton du paquet à l'origine du besoin de réponse.   *
*                error  = éventuelle indication d'erreur.                     *
*                                                                             *
*  Description : Définit l'en-tête du paquet pour une réponse au format JDWP. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_jdwp_packet_set_reply_header(GJdwpPacket *packet, uint32_t lastid, uint16_t error)
{
    set_jdwp_reply_header(&packet->header, packet->hblob,
                          0, lastid, error);

    packet->got = false;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet  = instance à consulter.                              *
*                payload = modèle de charge à copier.                         *
*                                                                             *
*  Description : Fournit l'adresse des charges utiles d'un paquet JDWP.       *
*                                                                             *
*  Retour      : Adresse des données d'une charge utile.                      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_jdwp_packet_set_payload(GJdwpPacket *packet, const jdwp_payload *payload)
{
    off_t len;                              /* Quantité max puis effective */

    len = sizeof(jdwp_payload);

    set_jdwp_payload(payload, packet->header.set, packet->header.command,
                     &G_JDWP_PACKET_GET_CLASS(packet)->sizes,
                     packet->pblob, &len);

    packet->header.length += len;

    update_jdwp_header_length(&packet->header, packet->hblob);

    packet->got = false;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = instance à consulter.                               *
*                                                                             *
*  Description : Fournit l'adresse des charges utiles d'un paquet JDWP.       *
*                                                                             *
*  Retour      : Adresse des données d'une charge utile.                      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

jdwp_payload *g_jdwp_packet_get_payload(GJdwpPacket *packet)
{
    return &packet->payload;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = instance à consulter.                               *
*                                                                             *
*  Description : Fournit l'adresse des charges utiles d'un paquet JDWP.       *
*                                                                             *
*  Retour      : Adresse des données d'une charge utile.                      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bin_t *g_jdwp_packet_get_pblob(GJdwpPacket *packet)
{
    return packet->pblob;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = instance à mettre à jour.                           *
*                set    = jeu de commandes concerné.                          *
*                cmd    = identifiant d'une commande donnée.                  *
*                                                                             *
*  Description : Recompose une charge utile à partir de ses données brutes.   *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_jdwp_packet_parse_payload(GJdwpPacket *packet, uint8_t set, uint8_t cmd)
{
    bool result;                            /* Bilan à retourner           */

    result = get_jdwp_payload(packet->pblob, packet->header.length - sizeof(jdwp_header)/* FIXME */,
                              set, cmd, &G_JDWP_PACKET_GET_CLASS(packet)->sizes, &packet->payload);

    if (result)
    {
        packet->header.set = set;
        packet->header.command = cmd;
    }

    packet->got = true;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : packet = instance à mettre à jour.                           *
*                                                                             *
*  Description : Libère la mémoire occupée par une charge utile de paquet.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_jdwp_packet_free_payload(GJdwpPacket *packet)
{
    if (packet->header.set == JDWP_CST_NONE || packet->header.command == JDWP_CMD_NONE)
        return;

    free_jdwp_payload(&packet->payload, packet->got,
                      packet->header.set, packet->header.command);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : reply  = instance de paquet à analyser.                      *
*                packet = instance de paquet de référence.                    *
*                                                                             *
*  Description : Détermine si un paquet est une réponse à un premier paquet.  *
*                                                                             *
*  Retour      : true si le paquet correspond à la réponse attendue.          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_jdwp_packet_is_reply(const GJdwpPacket *reply, const GJdwpPacket *packet)
{
    return (reply->header.id == packet->header.id
            && reply->header.flags & JDWP_FLAGS_REPLY);

}