rsp-low.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Low-level RSP routines for GDB, the GNU debugger.
  2. Copyright (C) 1988-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "common-defs.h"
  15. #include "rsp-low.h"
  16. /* See rsp-low.h. */
  17. int
  18. tohex (int nib)
  19. {
  20. if (nib < 10)
  21. return '0' + nib;
  22. else
  23. return 'a' + nib - 10;
  24. }
  25. /* Encode 64 bits in 16 chars of hex. */
  26. static const char hexchars[] = "0123456789abcdef";
  27. static int
  28. ishex (int ch, int *val)
  29. {
  30. if ((ch >= 'a') && (ch <= 'f'))
  31. {
  32. *val = ch - 'a' + 10;
  33. return 1;
  34. }
  35. if ((ch >= 'A') && (ch <= 'F'))
  36. {
  37. *val = ch - 'A' + 10;
  38. return 1;
  39. }
  40. if ((ch >= '0') && (ch <= '9'))
  41. {
  42. *val = ch - '0';
  43. return 1;
  44. }
  45. return 0;
  46. }
  47. /* See rsp-low.h. */
  48. char *
  49. pack_nibble (char *buf, int nibble)
  50. {
  51. *buf++ = hexchars[(nibble & 0x0f)];
  52. return buf;
  53. }
  54. /* See rsp-low.h. */
  55. char *
  56. pack_hex_byte (char *pkt, int byte)
  57. {
  58. *pkt++ = hexchars[(byte >> 4) & 0xf];
  59. *pkt++ = hexchars[(byte & 0xf)];
  60. return pkt;
  61. }
  62. /* See rsp-low.h. */
  63. const char *
  64. unpack_varlen_hex (const char *buff, /* packet to parse */
  65. ULONGEST *result)
  66. {
  67. int nibble;
  68. ULONGEST retval = 0;
  69. while (ishex (*buff, &nibble))
  70. {
  71. buff++;
  72. retval = retval << 4;
  73. retval |= nibble & 0x0f;
  74. }
  75. *result = retval;
  76. return buff;
  77. }
  78. /* See rsp-low.h. */
  79. std::string
  80. hex2str (const char *hex)
  81. {
  82. return hex2str (hex, strlen (hex));
  83. }
  84. /* See rsp-low.h. */
  85. std::string
  86. hex2str (const char *hex, int count)
  87. {
  88. std::string ret;
  89. ret.reserve (count);
  90. for (size_t i = 0; i < count; ++i)
  91. {
  92. if (hex[0] == '\0' || hex[1] == '\0')
  93. {
  94. /* Hex string is short, or of uneven length. Return what we
  95. have so far. */
  96. return ret;
  97. }
  98. ret += fromhex (hex[0]) * 16 + fromhex (hex[1]);
  99. hex += 2;
  100. }
  101. return ret;
  102. }
  103. /* See rsp-low.h. */
  104. int
  105. bin2hex (const gdb_byte *bin, char *hex, int count)
  106. {
  107. int i;
  108. for (i = 0; i < count; i++)
  109. {
  110. *hex++ = tohex ((*bin >> 4) & 0xf);
  111. *hex++ = tohex (*bin++ & 0xf);
  112. }
  113. *hex = 0;
  114. return i;
  115. }
  116. /* See rsp-low.h. */
  117. std::string
  118. bin2hex (const gdb_byte *bin, int count)
  119. {
  120. std::string ret;
  121. ret.reserve (count * 2);
  122. for (int i = 0; i < count; ++i)
  123. {
  124. ret += tohex ((*bin >> 4) & 0xf);
  125. ret += tohex (*bin++ & 0xf);
  126. }
  127. return ret;
  128. }
  129. /* Return whether byte B needs escaping when sent as part of binary data. */
  130. static int
  131. needs_escaping (gdb_byte b)
  132. {
  133. return b == '$' || b == '#' || b == '}' || b == '*';
  134. }
  135. /* See rsp-low.h. */
  136. int
  137. remote_escape_output (const gdb_byte *buffer, int len_units, int unit_size,
  138. gdb_byte *out_buf, int *out_len_units,
  139. int out_maxlen_bytes)
  140. {
  141. int input_unit_index, output_byte_index = 0, byte_index_in_unit;
  142. int number_escape_bytes_needed;
  143. /* Try to copy integral addressable memory units until
  144. (1) we run out of space or
  145. (2) we copied all of them. */
  146. for (input_unit_index = 0;
  147. input_unit_index < len_units;
  148. input_unit_index++)
  149. {
  150. /* Find out how many escape bytes we need for this unit. */
  151. number_escape_bytes_needed = 0;
  152. for (byte_index_in_unit = 0;
  153. byte_index_in_unit < unit_size;
  154. byte_index_in_unit++)
  155. {
  156. int idx = input_unit_index * unit_size + byte_index_in_unit;
  157. gdb_byte b = buffer[idx];
  158. if (needs_escaping (b))
  159. number_escape_bytes_needed++;
  160. }
  161. /* Check if we have room to fit this escaped unit. */
  162. if (output_byte_index + unit_size + number_escape_bytes_needed >
  163. out_maxlen_bytes)
  164. break;
  165. /* Copy the unit byte per byte, adding escapes. */
  166. for (byte_index_in_unit = 0;
  167. byte_index_in_unit < unit_size;
  168. byte_index_in_unit++)
  169. {
  170. int idx = input_unit_index * unit_size + byte_index_in_unit;
  171. gdb_byte b = buffer[idx];
  172. if (needs_escaping (b))
  173. {
  174. out_buf[output_byte_index++] = '}';
  175. out_buf[output_byte_index++] = b ^ 0x20;
  176. }
  177. else
  178. out_buf[output_byte_index++] = b;
  179. }
  180. }
  181. *out_len_units = input_unit_index;
  182. return output_byte_index;
  183. }
  184. /* See rsp-low.h. */
  185. int
  186. remote_unescape_input (const gdb_byte *buffer, int len,
  187. gdb_byte *out_buf, int out_maxlen)
  188. {
  189. int input_index, output_index;
  190. int escaped;
  191. output_index = 0;
  192. escaped = 0;
  193. for (input_index = 0; input_index < len; input_index++)
  194. {
  195. gdb_byte b = buffer[input_index];
  196. if (output_index + 1 > out_maxlen)
  197. error (_("Received too much data from the target."));
  198. if (escaped)
  199. {
  200. out_buf[output_index++] = b ^ 0x20;
  201. escaped = 0;
  202. }
  203. else if (b == '}')
  204. escaped = 1;
  205. else
  206. out_buf[output_index++] = b;
  207. }
  208. if (escaped)
  209. error (_("Unmatched escape character in target response."));
  210. return output_index;
  211. }