summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-gdb.c
blob: 253269fd30b9bd172fb498bb253162b8c81bb36d (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
/* packet-gdb.c
 * Routines for dissection of GDB's Remote Serial Protocol
 *
 * Copyright 2014, Martin Kaiser <martin@kaiser.cx>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * The GDB Remote Serial Protocol is used between an instance of the
 * GNU Debugger and a remote target such as an embedded system.
 * It can be run over TCP/IP or a serial line, we support only TCP/IP.
 *
 * The protocol specification is in Annex E of the GDB user manual
 * http://www.gnu.org/software/gdb/documentation/
 *
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/tvbparse.h>

enum {
    GDB_TOK_ACK,
    GDB_TOK_START,
    GDB_TOK_PAYLOAD,
    GDB_TOK_END,
    GDB_TOK_CHKSUM
};

static const value_string gdb_ack[] = {
    { '+', "Transmission successful" },
    { '-', "Transmission failed" },
    { 0, NULL }
};


void proto_register_gdb(void);
void proto_reg_handoff_gdb(void);

static dissector_handle_t gdb_handle;

static int proto_gdb = -1;

static gint ett_gdb = -1;

static int hf_gdb_ack = -1;
static int hf_gdb_start = -1;
static int hf_gdb_payload = -1;
static int hf_gdb_end = -1;
static int hf_gdb_chksum = -1;

static tvbparse_wanted_t *want;

static void
dissect_gdb_token(void *tvbparse_data, const void *wanted_data, tvbparse_elem_t *tok)
{
    proto_tree *tree;
    guint       token;

    if (!tok)   /* XXX - is this check necessary? */
        return;

    tree = (proto_tree *)tvbparse_data;
    token = GPOINTER_TO_UINT(wanted_data);

    /* XXX - check that tok->len is what we expect? */
    switch (token) {
        case GDB_TOK_ACK:
            proto_tree_add_item(tree, hf_gdb_ack,
                    tok->tvb, tok->offset, tok->len, ENC_ASCII|ENC_NA);
            break;
        case GDB_TOK_START:
            proto_tree_add_item(tree, hf_gdb_start,
                    tok->tvb, tok->offset, tok->len, ENC_ASCII);
            break;
        case GDB_TOK_PAYLOAD:
            proto_tree_add_item(tree, hf_gdb_payload,
                    tok->tvb, tok->offset, tok->len, ENC_NA);
            break;
        case GDB_TOK_END:
            proto_tree_add_item(tree, hf_gdb_end,
                    tok->tvb, tok->offset, tok->len, ENC_ASCII);
            break;
        case GDB_TOK_CHKSUM:
            proto_tree_add_item(tree, hf_gdb_chksum,
                    tok->tvb, tok->offset, tok->len, ENC_ASCII);
            break;
        default:
            break;
    }
}

static void init_gdb_parser(void) {
    tvbparse_wanted_t *want_ack;
    tvbparse_wanted_t *want_start;
    tvbparse_wanted_t *want_payload;
    tvbparse_wanted_t *want_end;
    tvbparse_wanted_t *want_chksum;

    want_ack = tvbparse_chars(-1, 1, 1, "+-",
            GUINT_TO_POINTER(GDB_TOK_ACK), NULL, dissect_gdb_token);
    want_start = tvbparse_chars(-1, 1, 1, "$",
            GUINT_TO_POINTER(GDB_TOK_START), NULL, dissect_gdb_token);
    want_payload = tvbparse_not_chars(-1, 1, 0, "$#",
            GUINT_TO_POINTER(GDB_TOK_PAYLOAD), NULL, dissect_gdb_token);
    want_end = tvbparse_chars(-1, 1, 1, "#",
            GUINT_TO_POINTER(GDB_TOK_END), NULL, dissect_gdb_token);
    want_chksum = tvbparse_chars(-1, 2, 2, "0123456789abcdefABCDEF",
            GUINT_TO_POINTER(GDB_TOK_CHKSUM), NULL, dissect_gdb_token);

    want = tvbparse_set_seq(-1, NULL, NULL, NULL,
            tvbparse_some(-1, 0, 1, NULL, NULL, NULL, want_ack),
            want_start, want_payload, want_end, want_chksum, NULL);
}


static void
dissect_gdb_packet(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    proto_item  *ti;
    proto_tree  *gdb_tree;
    tvbparse_t  *tt;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "GDB");
    col_clear(pinfo->cinfo, COL_INFO);

    ti = proto_tree_add_protocol_format(tree, proto_gdb,
            tvb, 0, tvb_reported_length(tvb), "GDB Remote Serial Protocol");
    gdb_tree = proto_item_add_subtree(ti, ett_gdb);

    /* XXX support multiple sub-trees */
    tt = tvbparse_init(pinfo->pool, tvb, 0, -1, (void *)gdb_tree, NULL);

    while(tvbparse_get(tt, want)) {
        ;
    }
}


static int
dissect_gdb_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
    gint      offset=0, offset_start;
    gint      pos;
    guint     packet_len;
    tvbuff_t *packet_tvb;

    while (tvb_captured_length_remaining(tvb, offset) > 0) {
        packet_tvb = NULL;
        offset_start = offset;
        pos = tvb_find_guint8(tvb, offset, -1, '#');
        if (pos != -1) {
            offset += pos;
            offset++; /* skip the hash sign */
            /* to have a complete packet, we need another two bytes
               for the checksum */
            if (tvb_bytes_exist(tvb, offset, 2)) {
                offset += 2;
                packet_len = offset-offset_start;
                packet_tvb = tvb_new_subset_length(tvb, offset_start,
                        packet_len);
            }
        }

        if (packet_tvb)
            dissect_gdb_packet(tvb, pinfo, tree);
        else {
            pinfo->desegment_offset = offset;
            pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
            return tvb_captured_length(tvb);
        }
    }
    return tvb_captured_length(tvb);
}


void
proto_register_gdb(void)
{
    static hf_register_info hf[] = {
        { &hf_gdb_ack,
          { "Acknowledge", "gdb.ack", FT_CHAR, BASE_HEX,
              VALS(gdb_ack), 0, NULL, HFILL } },
        { &hf_gdb_start,
          { "Start character", "gdb.start", FT_STRING, BASE_NONE,
              NULL, 0, NULL, HFILL } },
        { &hf_gdb_payload,
          { "Payload", "gdb.payload", FT_BYTES, BASE_NONE,
              NULL, 0, NULL, HFILL } },
        { &hf_gdb_end,
          { "Terminating character", "gdb.end", FT_STRING, BASE_NONE,
              NULL, 0, NULL, HFILL } },
        { &hf_gdb_chksum,
          { "Checksum", "gdb.chksum", FT_STRING, BASE_NONE,
              NULL, 0, NULL, HFILL } }
    };

    static gint *ett[] = {
        &ett_gdb
    };


    proto_gdb = proto_register_protocol("GDB Remote Serial Protocol", "GDB remote", "gdb");
    gdb_handle = register_dissector("gdb", dissect_gdb_tcp, proto_gdb);

    proto_register_field_array(proto_gdb, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    init_gdb_parser();
}


void
proto_reg_handoff_gdb(void)
{
    dissector_add_for_decode_as_with_preference("tcp.port", gdb_handle);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */