GDBserver

rsp-low.c
Go to the documentation of this file.
1 /* Low-level RSP routines for GDB, the GNU debugger.
2
3 Copyright (C) 1988-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "common-defs.h"
21 #include "rsp-low.h"
22
23 /* See rsp-low.h. */
24
25 int
26 fromhex (int a)
27 {
28 if (a >= '0' && a <= '9')
29 return a - '0';
30 else if (a >= 'a' && a <= 'f')
31 return a - 'a' + 10;
32 else if (a >= 'A' && a <= 'F')
33 return a - 'A' + 10;
34 else
35 error (_("Reply contains invalid hex digit %d"), a);
36 }
37
38 /* See rsp-low.h. */
39
40 int
41 tohex (int nib)
42 {
43 if (nib < 10)
44 return '0' + nib;
45 else
46 return 'a' + nib - 10;
47 }
48
49 /* Encode 64 bits in 16 chars of hex. */
50
51 static const char hexchars[] = "0123456789abcdef";
52
53 static int
54 ishex (int ch, int *val)
55 {
56 if ((ch >= 'a') && (ch <= 'f'))
57 {
58 *val = ch - 'a' + 10;
59 return 1;
60 }
61 if ((ch >= 'A') && (ch <= 'F'))
62 {
63 *val = ch - 'A' + 10;
64 return 1;
65 }
66 if ((ch >= '0') && (ch <= '9'))
67 {
68 *val = ch - '0';
69 return 1;
70 }
71 return 0;
72 }
73
74 /* See rsp-low.h. */
75
76 char *
77 pack_nibble (char *buf, int nibble)
78 {
79 *buf++ = hexchars[(nibble & 0x0f)];
80 return buf;
81 }
82
83 /* See rsp-low.h. */
84
85 char *
86 pack_hex_byte (char *pkt, int byte)
87 {
88 *pkt++ = hexchars[(byte >> 4) & 0xf];
89 *pkt++ = hexchars[(byte & 0xf)];
90 return pkt;
91 }
92
93 /* See rsp-low.h. */
94
95 const char *
96 unpack_varlen_hex (const char *buff, /* packet to parse */
97 ULONGEST *result)
98 {
99 int nibble;
100 ULONGEST retval = 0;
101
102 while (ishex (*buff, &nibble))
103 {
104 buff++;
105 retval = retval << 4;
106 retval |= nibble & 0x0f;
107 }
108 *result = retval;
109 return buff;
110 }
111
112 /* See rsp-low.h. */
113
114 int
115 hex2bin (const char *hex, gdb_byte *bin, int count)
116 {
117 int i;
118
119 for (i = 0; i < count; i++)
120 {
121 if (hex[0] == 0 || hex[1] == 0)
122 {
123 /* Hex string is short, or of uneven length.
124 Return the count that has been converted so far. */
125 return i;
126 }
127 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
128 hex += 2;
129 }
130 return i;
131 }
132
133 /* See rsp-low.h. */
134
135 gdb::byte_vector
136 hex2bin (const char *hex)
137 {
138 size_t bin_len = strlen (hex) / 2;
139 gdb::byte_vector bin (bin_len);
140
141 hex2bin (hex, bin.data (), bin_len);
142
143 return bin;
144 }
145
146 /* See rsp-low.h. */
147
148 std::string
149 hex2str (const char *hex)
150 {
151 std::string ret;
152 size_t len = strlen (hex);
153
154 ret.reserve (len / 2);
155 for (size_t i = 0; i < len; ++i)
156 {
157 if (hex[0] == '\0' || hex[1] == '\0')
158 {
159 /* Hex string is short, or of uneven length. Return what we
160 have so far. */
161 return ret;
162 }
163 ret += fromhex (hex[0]) * 16 + fromhex (hex[1]);
164 hex += 2;
165 }
166
167 return ret;
168 }
169
170 /* See rsp-low.h. */
171
172 int
173 bin2hex (const gdb_byte *bin, char *hex, int count)
174 {
175 int i;
176
177 for (i = 0; i < count; i++)
178 {
179 *hex++ = tohex ((*bin >> 4) & 0xf);
180 *hex++ = tohex (*bin++ & 0xf);
181 }
182 *hex = 0;
183 return i;
184 }
185
186 /* See rsp-low.h. */
187
188 std::string
189 bin2hex (const gdb_byte *bin, int count)
190 {
191 std::string ret;
192
193 ret.reserve (count * 2);
194 for (int i = 0; i < count; ++i)
195 {
196 ret += tohex ((*bin >> 4) & 0xf);
197 ret += tohex (*bin++ & 0xf);
198 }
199
200 return ret;
201 }
202
203 /* Return whether byte B needs escaping when sent as part of binary data. */
204
205 static int
206 needs_escaping (gdb_byte b)
207 {
208 return b == '$' || b == '#' || b == '}' || b == '*';
209 }
210
211 /* See rsp-low.h. */
212
213 int
214 remote_escape_output (const gdb_byte *buffer, int len_units, int unit_size,
215 gdb_byte *out_buf, int *out_len_units,
216 int out_maxlen_bytes)
217 {
218 int input_unit_index, output_byte_index = 0, byte_index_in_unit;
219 int number_escape_bytes_needed;
220
221 /* Try to copy integral addressable memory units until
222 (1) we run out of space or
223 (2) we copied all of them. */
224 for (input_unit_index = 0;
225 input_unit_index < len_units;
226 input_unit_index++)
227 {
228 /* Find out how many escape bytes we need for this unit. */
229 number_escape_bytes_needed = 0;
230 for (byte_index_in_unit = 0;
231 byte_index_in_unit < unit_size;
232 byte_index_in_unit++)
233 {
234 int idx = input_unit_index * unit_size + byte_index_in_unit;
235 gdb_byte b = buffer[idx];
236 if (needs_escaping (b))
237 number_escape_bytes_needed++;
238 }
239
240 /* Check if we have room to fit this escaped unit. */
241 if (output_byte_index + unit_size + number_escape_bytes_needed >
242 out_maxlen_bytes)
243 break;
244
245 /* Copy the unit byte per byte, adding escapes. */
246 for (byte_index_in_unit = 0;
247 byte_index_in_unit < unit_size;
248 byte_index_in_unit++)
249 {
250 int idx = input_unit_index * unit_size + byte_index_in_unit;
251 gdb_byte b = buffer[idx];
252 if (needs_escaping (b))
253 {
254 out_buf[output_byte_index++] = '}';
255 out_buf[output_byte_index++] = b ^ 0x20;
256 }
257 else
258 out_buf[output_byte_index++] = b;
259 }
260 }
261
262 *out_len_units = input_unit_index;
263 return output_byte_index;
264 }
265
266 /* See rsp-low.h. */
267
268 int
269 remote_unescape_input (const gdb_byte *buffer, int len,
270 gdb_byte *out_buf, int out_maxlen)
271 {
272 int input_index, output_index;
273 int escaped;
274
275 output_index = 0;
276 escaped = 0;
277 for (input_index = 0; input_index < len; input_index++)
278 {
279 gdb_byte b = buffer[input_index];
280
281 if (output_index + 1 > out_maxlen)
282 error (_("Received too much data from the target."));
283
284 if (escaped)
285 {
286 out_buf[output_index++] = b ^ 0x20;
287 escaped = 0;
288 }
289 else if (b == '}')
290 escaped = 1;
291 else
292 out_buf[output_index++] = b;
293 }
294
295 if (escaped)
296 error (_("Unmatched escape character in target response."));
297
298 return output_index;
299 }
300
string
const char * string
Definition: signals.c:50
remote_unescape_input
int remote_unescape_input(const gdb_byte *buffer, int len, gdb_byte *out_buf, int out_maxlen)
Definition: rsp-low.c:269
bin2hex
int bin2hex(const gdb_byte *bin, char *hex, int count)
Definition: rsp-low.c:173
tohex
int tohex(int nib)
Definition: rsp-low.c:41
remote_escape_output
int remote_escape_output(const gdb_byte *buffer, int len_units, int unit_size, gdb_byte *out_buf, int *out_len_units, int out_maxlen_bytes)
Definition: rsp-low.c:214
_
#define _(String)
Definition: gdb_locale.h:35
rsp-low.h
common-defs.h
pack_nibble
char * pack_nibble(char *buf, int nibble)
Definition: rsp-low.c:77
fromhex
int fromhex(int a)
Definition: rsp-low.c:26
gdb_byte
bfd_byte gdb_byte
Definition: common-types.h:38
hex2str
std::string hex2str(const char *hex)
Definition: rsp-low.c:149
buffer
Definition: buffer.h:23
pack_hex_byte
char * pack_hex_byte(char *pkt, int byte)
Definition: rsp-low.c:86
gdb::byte_vector
gdb::def_vector< gdb_byte > byte_vector
Definition: byte-vector.h:58
hexchars
static const char hexchars[]
Definition: rsp-low.c:51
ULONGEST
unsigned long long ULONGEST
Definition: common-types.h:53
needs_escaping
static int needs_escaping(gdb_byte b)
Definition: rsp-low.c:206
hex2bin
int hex2bin(const char *hex, gdb_byte *bin, int count)
Definition: rsp-low.c:115
unpack_varlen_hex
const char * unpack_varlen_hex(const char *buff, ULONGEST *result)
Definition: rsp-low.c:96
error
void error(const char *fmt,...)
Definition: errors.c:38
ishex
static int ishex(int ch, int *val)
Definition: rsp-low.c:54