Mention support for AMD/znver5 in GAS
[binutils-gdb.git] / gdbsupport / rsp-low.cc
blob37dce9d5c747546d31537d06cc3ed57ce0976e87
1 /* Low-level RSP routines for GDB, the GNU debugger.
3 Copyright (C) 1988-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "common-defs.h"
21 #include "rsp-low.h"
23 /* See rsp-low.h. */
25 int
26 tohex (int nib)
28 if (nib < 10)
29 return '0' + nib;
30 else
31 return 'a' + nib - 10;
34 /* Encode 64 bits in 16 chars of hex. */
36 static const char hexchars[] = "0123456789abcdef";
38 static int
39 ishex (int ch, int *val)
41 if ((ch >= 'a') && (ch <= 'f'))
43 *val = ch - 'a' + 10;
44 return 1;
46 if ((ch >= 'A') && (ch <= 'F'))
48 *val = ch - 'A' + 10;
49 return 1;
51 if ((ch >= '0') && (ch <= '9'))
53 *val = ch - '0';
54 return 1;
56 return 0;
59 /* See rsp-low.h. */
61 char *
62 pack_nibble (char *buf, int nibble)
64 *buf++ = hexchars[(nibble & 0x0f)];
65 return buf;
68 /* See rsp-low.h. */
70 char *
71 pack_hex_byte (char *pkt, int byte)
73 *pkt++ = hexchars[(byte >> 4) & 0xf];
74 *pkt++ = hexchars[(byte & 0xf)];
75 return pkt;
78 /* See rsp-low.h. */
80 const char *
81 unpack_varlen_hex (const char *buff, /* packet to parse */
82 ULONGEST *result)
84 int nibble;
85 ULONGEST retval = 0;
87 while (ishex (*buff, &nibble))
89 buff++;
90 retval = retval << 4;
91 retval |= nibble & 0x0f;
93 *result = retval;
94 return buff;
97 /* See rsp-low.h. */
99 std::string
100 hex2str (const char *hex)
102 return hex2str (hex, strlen (hex));
105 /* See rsp-low.h. */
107 std::string
108 hex2str (const char *hex, int count)
110 std::string ret;
112 ret.reserve (count);
113 for (size_t i = 0; i < count; ++i)
115 if (hex[0] == '\0' || hex[1] == '\0')
117 /* Hex string is short, or of uneven length. Return what we
118 have so far. */
119 return ret;
121 ret += fromhex (hex[0]) * 16 + fromhex (hex[1]);
122 hex += 2;
125 return ret;
128 /* See rsp-low.h. */
131 bin2hex (const gdb_byte *bin, char *hex, int count)
133 int i;
135 for (i = 0; i < count; i++)
137 *hex++ = tohex ((*bin >> 4) & 0xf);
138 *hex++ = tohex (*bin++ & 0xf);
140 *hex = 0;
141 return i;
144 /* See rsp-low.h. */
147 bin2hex (gdb::array_view<gdb_byte> bin, char *hex)
149 return bin2hex (bin.data (), hex, bin.size ());
152 /* See rsp-low.h. */
154 std::string
155 bin2hex (const gdb_byte *bin, int count)
157 std::string ret;
159 ret.reserve (count * 2);
160 for (int i = 0; i < count; ++i)
162 ret += tohex ((*bin >> 4) & 0xf);
163 ret += tohex (*bin++ & 0xf);
166 return ret;
169 /* Return whether byte B needs escaping when sent as part of binary data. */
171 static int
172 needs_escaping (gdb_byte b)
174 return b == '$' || b == '#' || b == '}' || b == '*';
177 /* See rsp-low.h. */
180 remote_escape_output (const gdb_byte *buffer, int len_units, int unit_size,
181 gdb_byte *out_buf, int *out_len_units,
182 int out_maxlen_bytes)
184 int input_unit_index, output_byte_index = 0, byte_index_in_unit;
185 int number_escape_bytes_needed;
187 /* Try to copy integral addressable memory units until
188 (1) we run out of space or
189 (2) we copied all of them. */
190 for (input_unit_index = 0;
191 input_unit_index < len_units;
192 input_unit_index++)
194 /* Find out how many escape bytes we need for this unit. */
195 number_escape_bytes_needed = 0;
196 for (byte_index_in_unit = 0;
197 byte_index_in_unit < unit_size;
198 byte_index_in_unit++)
200 int idx = input_unit_index * unit_size + byte_index_in_unit;
201 gdb_byte b = buffer[idx];
202 if (needs_escaping (b))
203 number_escape_bytes_needed++;
206 /* Check if we have room to fit this escaped unit. */
207 if (output_byte_index + unit_size + number_escape_bytes_needed >
208 out_maxlen_bytes)
209 break;
211 /* Copy the unit byte per byte, adding escapes. */
212 for (byte_index_in_unit = 0;
213 byte_index_in_unit < unit_size;
214 byte_index_in_unit++)
216 int idx = input_unit_index * unit_size + byte_index_in_unit;
217 gdb_byte b = buffer[idx];
218 if (needs_escaping (b))
220 out_buf[output_byte_index++] = '}';
221 out_buf[output_byte_index++] = b ^ 0x20;
223 else
224 out_buf[output_byte_index++] = b;
228 *out_len_units = input_unit_index;
229 return output_byte_index;
232 /* See rsp-low.h. */
235 remote_unescape_input (const gdb_byte *buffer, int len,
236 gdb_byte *out_buf, int out_maxlen)
238 int input_index, output_index;
239 int escaped;
241 output_index = 0;
242 escaped = 0;
243 for (input_index = 0; input_index < len; input_index++)
245 gdb_byte b = buffer[input_index];
247 if (output_index + 1 > out_maxlen)
248 error (_("Received too much data from the target."));
250 if (escaped)
252 out_buf[output_index++] = b ^ 0x20;
253 escaped = 0;
255 else if (b == '}')
256 escaped = 1;
257 else
258 out_buf[output_index++] = b;
261 if (escaped)
262 error (_("Unmatched escape character in target response."));
264 return output_index;