2 * Routines for dissection of GDB's Remote Serial Protocol
4 * Copyright 2014, Martin Kaiser <martin@kaiser.cx>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
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.
18 * The protocol specification is in Annex E of the GDB user manual
19 * http://www.gnu.org/software/gdb/documentation/
25 #include <epan/packet.h>
26 #include <epan/tvbparse.h>
36 static const value_string gdb_ack[] = {
37 { '+', "Transmission successful" },
38 { '-', "Transmission failed" },
43 void proto_register_gdb(void);
44 void proto_reg_handoff_gdb(void);
46 static int proto_gdb = -1;
48 static gint ett_gdb = -1;
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;
56 static tvbparse_wanted_t *want;
59 dissect_gdb_token(void *tvbparse_data, const void *wanted_data, tvbparse_elem_t *tok)
64 if (!tok) /* XXX - is this check necessary? */
67 tree = (proto_tree *)tvbparse_data;
68 token = GPOINTER_TO_UINT(wanted_data);
70 /* XXX - check that tok->len is what we expect? */
73 proto_tree_add_item(tree, hf_gdb_ack,
74 tok->tvb, tok->offset, tok->len, ENC_ASCII|ENC_NA);
77 proto_tree_add_item(tree, hf_gdb_start,
78 tok->tvb, tok->offset, tok->len, ENC_ASCII|ENC_NA);
81 proto_tree_add_item(tree, hf_gdb_payload,
82 tok->tvb, tok->offset, tok->len, ENC_NA);
85 proto_tree_add_item(tree, hf_gdb_end,
86 tok->tvb, tok->offset, tok->len, ENC_ASCII|ENC_NA);
89 proto_tree_add_item(tree, hf_gdb_chksum,
90 tok->tvb, tok->offset, tok->len, ENC_ASCII|ENC_NA);
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;
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);
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);
122 dissect_gdb_packet(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
125 proto_tree *gdb_tree;
128 col_set_str(pinfo->cinfo, COL_PROTOCOL, "GDB");
129 col_clear(pinfo->cinfo, COL_INFO);
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);
135 /* XXX support multiple sub-trees */
136 tt = tvbparse_init(tvb, 0, -1, (void *)gdb_tree, NULL);
138 while(tvbparse_get(tt, want)) {
145 dissect_gdb_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
147 gint offset=0, offset_start;
150 tvbuff_t *packet_tvb;
152 while (tvb_captured_length_remaining(tvb, offset) > 0) {
154 offset_start = offset;
155 pos = tvb_find_guint8(tvb, offset, -1, '#');
158 offset++; /* skip the hash sign */
159 /* to have a complete packet, we need another two bytes
161 if (tvb_bytes_exist(tvb, offset, 2)) {
163 packet_len = offset-offset_start;
164 packet_tvb = tvb_new_subset_length(tvb, offset_start,
170 dissect_gdb_packet(tvb, pinfo, tree);
172 pinfo->desegment_offset = offset;
173 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
174 return tvb_captured_length(tvb);
177 return tvb_captured_length(tvb);
182 proto_register_gdb(void)
184 static hf_register_info hf[] = {
186 { "Acknowledge", "gdb.ack", FT_CHAR, BASE_HEX,
187 VALS(gdb_ack), 0, NULL, HFILL } },
189 { "Start character", "gdb.start", FT_STRING, BASE_NONE,
190 NULL, 0, NULL, HFILL } },
192 { "Payload", "gdb.payload", FT_BYTES, BASE_NONE,
193 NULL, 0, NULL, HFILL } },
195 { "Terminating character", "gdb.end", FT_STRING, BASE_NONE,
196 NULL, 0, NULL, HFILL } },
198 { "Checksum", "gdb.chksum", FT_STRING, BASE_NONE,
199 NULL, 0, NULL, HFILL } }
202 static gint *ett[] = {
207 proto_gdb = proto_register_protocol("GDB Remote Serial Protocol", "GDB remote", "gdb");
209 proto_register_field_array(proto_gdb, hf, array_length(hf));
210 proto_register_subtree_array(ett, array_length(ett));
217 proto_reg_handoff_gdb(void)
219 dissector_handle_t gdb_handle;
221 gdb_handle = create_dissector_handle(dissect_gdb_tcp, proto_gdb);
223 dissector_add_for_decode_as_with_preference("tcp.port", gdb_handle);
227 * Editor modelines - http://www.wireshark.org/tools/modelines.html
232 * indent-tabs-mode: nil
235 * vi: set shiftwidth=4 tabstop=8 expandtab:
236 * :indentSize=4:tabSize=8:noTabs=true: