checkAPIs.pl: support for new-style dissectors in check_hf_entries
[metze/wireshark/wip.git] / epan / dissectors / packet-gdb.c
1 /* packet-gdb.c
2  * Routines for dissection of GDB's Remote Serial Protocol
3  *
4  * Copyright 2014, Martin Kaiser <martin@kaiser.cx>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12
13 /*
14  * The GDB Remote Serial Protocol is used between an instance of the
15  * GNU Debugger and a remote target such as an embedded system.
16  * It can be run over TCP/IP or a serial line, we support only TCP/IP.
17  *
18  * The protocol specification is in Annex E of the GDB user manual
19  * http://www.gnu.org/software/gdb/documentation/
20  *
21  */
22
23 #include "config.h"
24
25 #include <epan/packet.h>
26 #include <epan/tvbparse.h>
27
28 enum {
29     GDB_TOK_ACK,
30     GDB_TOK_START,
31     GDB_TOK_PAYLOAD,
32     GDB_TOK_END,
33     GDB_TOK_CHKSUM
34 };
35
36 static const value_string gdb_ack[] = {
37     { '+', "Transmission successful" },
38     { '-', "Transmission failed" },
39     { 0, NULL }
40 };
41
42
43 void proto_register_gdb(void);
44 void proto_reg_handoff_gdb(void);
45
46 static int proto_gdb = -1;
47
48 static gint ett_gdb = -1;
49
50 static int hf_gdb_ack = -1;
51 static int hf_gdb_start = -1;
52 static int hf_gdb_payload = -1;
53 static int hf_gdb_end = -1;
54 static int hf_gdb_chksum = -1;
55
56 static tvbparse_wanted_t *want;
57
58 static void
59 dissect_gdb_token(void *tvbparse_data, const void *wanted_data, tvbparse_elem_t *tok)
60 {
61     proto_tree *tree;
62     guint       token;
63
64     if (!tok)   /* XXX - is this check necessary? */
65         return;
66
67     tree = (proto_tree *)tvbparse_data;
68     token = GPOINTER_TO_UINT(wanted_data);
69
70     /* XXX - check that tok->len is what we expect? */
71     switch (token) {
72         case GDB_TOK_ACK:
73             proto_tree_add_item(tree, hf_gdb_ack,
74                     tok->tvb, tok->offset, tok->len, ENC_ASCII|ENC_NA);
75             break;
76         case GDB_TOK_START:
77             proto_tree_add_item(tree, hf_gdb_start,
78                     tok->tvb, tok->offset, tok->len, ENC_ASCII|ENC_NA);
79             break;
80         case GDB_TOK_PAYLOAD:
81             proto_tree_add_item(tree, hf_gdb_payload,
82                     tok->tvb, tok->offset, tok->len, ENC_NA);
83             break;
84         case GDB_TOK_END:
85             proto_tree_add_item(tree, hf_gdb_end,
86                     tok->tvb, tok->offset, tok->len, ENC_ASCII|ENC_NA);
87             break;
88         case GDB_TOK_CHKSUM:
89             proto_tree_add_item(tree, hf_gdb_chksum,
90                     tok->tvb, tok->offset, tok->len, ENC_ASCII|ENC_NA);
91             break;
92         default:
93             break;
94     }
95 }
96
97 static void init_gdb_parser(void) {
98     tvbparse_wanted_t *want_ack;
99     tvbparse_wanted_t *want_start;
100     tvbparse_wanted_t *want_payload;
101     tvbparse_wanted_t *want_end;
102     tvbparse_wanted_t *want_chksum;
103
104     want_ack = tvbparse_chars(-1, 1, 1, "+-",
105             GUINT_TO_POINTER(GDB_TOK_ACK), NULL, dissect_gdb_token);
106     want_start = tvbparse_chars(-1, 1, 1, "$",
107             GUINT_TO_POINTER(GDB_TOK_START), NULL, dissect_gdb_token);
108     want_payload = tvbparse_not_chars(-1, 1, 0, "$#",
109             GUINT_TO_POINTER(GDB_TOK_PAYLOAD), NULL, dissect_gdb_token);
110     want_end = tvbparse_chars(-1, 1, 1, "#",
111             GUINT_TO_POINTER(GDB_TOK_END), NULL, dissect_gdb_token);
112     want_chksum = tvbparse_chars(-1, 2, 2, "0123456789abcdefABCDEF",
113             GUINT_TO_POINTER(GDB_TOK_CHKSUM), NULL, dissect_gdb_token);
114
115     want = tvbparse_set_seq(-1, NULL, NULL, NULL,
116             tvbparse_some(-1, 0, 1, NULL, NULL, NULL, want_ack),
117             want_start, want_payload, want_end, want_chksum, NULL);
118 }
119
120
121 static void
122 dissect_gdb_packet(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
123 {
124     proto_item  *ti;
125     proto_tree  *gdb_tree;
126     tvbparse_t  *tt;
127
128     col_set_str(pinfo->cinfo, COL_PROTOCOL, "GDB");
129     col_clear(pinfo->cinfo, COL_INFO);
130
131     ti = proto_tree_add_protocol_format(tree, proto_gdb,
132             tvb, 0, tvb_reported_length(tvb), "GDB Remote Serial Protocol");
133     gdb_tree = proto_item_add_subtree(ti, ett_gdb);
134
135     /* XXX support multiple sub-trees */
136     tt = tvbparse_init(tvb, 0, -1, (void *)gdb_tree, NULL);
137
138     while(tvbparse_get(tt, want)) {
139         ;
140     }
141 }
142
143
144 static int
145 dissect_gdb_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
146 {
147     gint      offset=0, offset_start;
148     gint      pos;
149     guint     packet_len;
150     tvbuff_t *packet_tvb;
151
152     while (tvb_captured_length_remaining(tvb, offset) > 0) {
153         packet_tvb = NULL;
154         offset_start = offset;
155         pos = tvb_find_guint8(tvb, offset, -1, '#');
156         if (pos != -1) {
157             offset += pos;
158             offset++; /* skip the hash sign */
159             /* to have a complete packet, we need another two bytes
160                for the checksum */
161             if (tvb_bytes_exist(tvb, offset, 2)) {
162                 offset += 2;
163                 packet_len = offset-offset_start;
164                 packet_tvb = tvb_new_subset_length(tvb, offset_start,
165                         packet_len);
166             }
167         }
168
169         if (packet_tvb)
170             dissect_gdb_packet(tvb, pinfo, tree);
171         else {
172             pinfo->desegment_offset = offset;
173             pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
174             return tvb_captured_length(tvb);
175         }
176     }
177     return tvb_captured_length(tvb);
178 }
179
180
181 void
182 proto_register_gdb(void)
183 {
184     static hf_register_info hf[] = {
185         { &hf_gdb_ack,
186           { "Acknowledge", "gdb.ack", FT_CHAR, BASE_HEX,
187               VALS(gdb_ack), 0, NULL, HFILL } },
188         { &hf_gdb_start,
189           { "Start character", "gdb.start", FT_STRING, BASE_NONE,
190               NULL, 0, NULL, HFILL } },
191         { &hf_gdb_payload,
192           { "Payload", "gdb.payload", FT_BYTES, BASE_NONE,
193               NULL, 0, NULL, HFILL } },
194         { &hf_gdb_end,
195           { "Terminating character", "gdb.end", FT_STRING, BASE_NONE,
196               NULL, 0, NULL, HFILL } },
197         { &hf_gdb_chksum,
198           { "Checksum", "gdb.chksum", FT_STRING, BASE_NONE,
199               NULL, 0, NULL, HFILL } }
200     };
201
202     static gint *ett[] = {
203         &ett_gdb
204     };
205
206
207     proto_gdb = proto_register_protocol("GDB Remote Serial Protocol", "GDB remote", "gdb");
208
209     proto_register_field_array(proto_gdb, hf, array_length(hf));
210     proto_register_subtree_array(ett, array_length(ett));
211
212     init_gdb_parser();
213 }
214
215
216 void
217 proto_reg_handoff_gdb(void)
218 {
219     dissector_handle_t  gdb_handle;
220
221     gdb_handle = create_dissector_handle(dissect_gdb_tcp, proto_gdb);
222
223     dissector_add_for_decode_as_with_preference("tcp.port", gdb_handle);
224 }
225
226 /*
227  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
228  *
229  * Local variables:
230  * c-basic-offset: 4
231  * tab-width: 8
232  * indent-tabs-mode: nil
233  * End:
234  *
235  * vi: set shiftwidth=4 tabstop=8 expandtab:
236  * :indentSize=4:tabSize=8:noTabs=true:
237  */