You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gdbstub.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /**
  21. * @file
  22. *
  23. * GDB stub for remote debugging
  24. *
  25. */
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <byteswap.h>
  31. #include <ipxe/gdbstub.h>
  32. #include "gdbmach.h"
  33. enum {
  34. POSIX_EINVAL = 0x1c, /* used to report bad arguments to GDB */
  35. SIZEOF_PAYLOAD = 256, /* buffer size of GDB payload data */
  36. };
  37. struct gdbstub {
  38. struct gdb_transport *trans;
  39. int exit_handler; /* leave interrupt handler */
  40. int signo;
  41. gdbreg_t *regs;
  42. void ( * parse ) ( struct gdbstub *stub, char ch );
  43. uint8_t cksum1;
  44. /* Buffer for payload data when parsing a packet. Once the
  45. * packet has been received, this buffer is used to hold
  46. * the reply payload. */
  47. char buf [ SIZEOF_PAYLOAD + 4 ]; /* $...PAYLOAD...#XX */
  48. char *payload; /* start of payload */
  49. int len; /* length of payload */
  50. };
  51. /* Packet parser states */
  52. static void gdbstub_state_new ( struct gdbstub *stub, char ch );
  53. static void gdbstub_state_data ( struct gdbstub *stub, char ch );
  54. static void gdbstub_state_cksum1 ( struct gdbstub *stub, char ch );
  55. static void gdbstub_state_cksum2 ( struct gdbstub *stub, char ch );
  56. static void gdbstub_state_wait_ack ( struct gdbstub *stub, char ch );
  57. static uint8_t gdbstub_from_hex_digit ( char ch ) {
  58. return ( isdigit ( ch ) ? ch - '0' : tolower ( ch ) - 'a' + 0xa ) & 0xf;
  59. }
  60. static uint8_t gdbstub_to_hex_digit ( uint8_t b ) {
  61. b &= 0xf;
  62. return ( b < 0xa ? '0' : 'a' - 0xa ) + b;
  63. }
  64. /*
  65. * To make reading/writing device memory atomic, we check for
  66. * 2- or 4-byte aligned operations and handle them specially.
  67. */
  68. static void gdbstub_from_hex_buf ( char *dst, char *src, int lenbytes ) {
  69. if ( lenbytes == 2 && ( ( unsigned long ) dst & 0x1 ) == 0 ) {
  70. uint16_t i = gdbstub_from_hex_digit ( src [ 2 ] ) << 12 |
  71. gdbstub_from_hex_digit ( src [ 3 ] ) << 8 |
  72. gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
  73. gdbstub_from_hex_digit ( src [ 1 ] );
  74. * ( uint16_t * ) dst = cpu_to_le16 ( i );
  75. } else if ( lenbytes == 4 && ( ( unsigned long ) dst & 0x3 ) == 0 ) {
  76. uint32_t i = gdbstub_from_hex_digit ( src [ 6 ] ) << 28 |
  77. gdbstub_from_hex_digit ( src [ 7 ] ) << 24 |
  78. gdbstub_from_hex_digit ( src [ 4 ] ) << 20 |
  79. gdbstub_from_hex_digit ( src [ 5 ] ) << 16 |
  80. gdbstub_from_hex_digit ( src [ 2 ] ) << 12 |
  81. gdbstub_from_hex_digit ( src [ 3 ] ) << 8 |
  82. gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
  83. gdbstub_from_hex_digit ( src [ 1 ] );
  84. * ( uint32_t * ) dst = cpu_to_le32 ( i );
  85. } else {
  86. while ( lenbytes-- > 0 ) {
  87. *dst++ = gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
  88. gdbstub_from_hex_digit ( src [ 1 ] );
  89. src += 2;
  90. }
  91. }
  92. }
  93. static void gdbstub_to_hex_buf ( char *dst, char *src, int lenbytes ) {
  94. if ( lenbytes == 2 && ( ( unsigned long ) src & 0x1 ) == 0 ) {
  95. uint16_t i = cpu_to_le16 ( * ( uint16_t * ) src );
  96. dst [ 0 ] = gdbstub_to_hex_digit ( i >> 4 );
  97. dst [ 1 ] = gdbstub_to_hex_digit ( i );
  98. dst [ 2 ] = gdbstub_to_hex_digit ( i >> 12 );
  99. dst [ 3 ] = gdbstub_to_hex_digit ( i >> 8 );
  100. } else if ( lenbytes == 4 && ( ( unsigned long ) src & 0x3 ) == 0 ) {
  101. uint32_t i = cpu_to_le32 ( * ( uint32_t * ) src );
  102. dst [ 0 ] = gdbstub_to_hex_digit ( i >> 4 );
  103. dst [ 1 ] = gdbstub_to_hex_digit ( i );
  104. dst [ 2 ] = gdbstub_to_hex_digit ( i >> 12 );
  105. dst [ 3 ] = gdbstub_to_hex_digit ( i >> 8 );
  106. dst [ 4 ] = gdbstub_to_hex_digit ( i >> 20 );
  107. dst [ 5 ] = gdbstub_to_hex_digit ( i >> 16);
  108. dst [ 6 ] = gdbstub_to_hex_digit ( i >> 28 );
  109. dst [ 7 ] = gdbstub_to_hex_digit ( i >> 24 );
  110. } else {
  111. while ( lenbytes-- > 0 ) {
  112. *dst++ = gdbstub_to_hex_digit ( *src >> 4 );
  113. *dst++ = gdbstub_to_hex_digit ( *src );
  114. src++;
  115. }
  116. }
  117. }
  118. static uint8_t gdbstub_cksum ( char *data, int len ) {
  119. uint8_t cksum = 0;
  120. while ( len-- > 0 ) {
  121. cksum += ( uint8_t ) *data++;
  122. }
  123. return cksum;
  124. }
  125. static void gdbstub_tx_packet ( struct gdbstub *stub ) {
  126. uint8_t cksum = gdbstub_cksum ( stub->payload, stub->len );
  127. stub->buf [ 0 ] = '$';
  128. stub->buf [ stub->len + 1 ] = '#';
  129. stub->buf [ stub->len + 2 ] = gdbstub_to_hex_digit ( cksum >> 4 );
  130. stub->buf [ stub->len + 3 ] = gdbstub_to_hex_digit ( cksum );
  131. stub->trans->send ( stub->buf, stub->len + 4 );
  132. stub->parse = gdbstub_state_wait_ack;
  133. }
  134. /* GDB commands */
  135. static void gdbstub_send_ok ( struct gdbstub *stub ) {
  136. stub->payload [ 0 ] = 'O';
  137. stub->payload [ 1 ] = 'K';
  138. stub->len = 2;
  139. gdbstub_tx_packet ( stub );
  140. }
  141. static void gdbstub_send_num_packet ( struct gdbstub *stub, char reply, int num ) {
  142. stub->payload [ 0 ] = reply;
  143. stub->payload [ 1 ] = gdbstub_to_hex_digit ( ( char ) num >> 4 );
  144. stub->payload [ 2 ] = gdbstub_to_hex_digit ( ( char ) num );
  145. stub->len = 3;
  146. gdbstub_tx_packet ( stub );
  147. }
  148. /* Format is arg1,arg2,...,argn:data where argn are hex integers and data is not an argument */
  149. static int gdbstub_get_packet_args ( struct gdbstub *stub, unsigned long *args, int nargs, int *stop_idx ) {
  150. int i;
  151. char ch = 0;
  152. int argc = 0;
  153. unsigned long val = 0;
  154. for ( i = 1; i < stub->len && argc < nargs; i++ ) {
  155. ch = stub->payload [ i ];
  156. if ( ch == ':' ) {
  157. break;
  158. } else if ( ch == ',' ) {
  159. args [ argc++ ] = val;
  160. val = 0;
  161. } else {
  162. val = ( val << 4 ) | gdbstub_from_hex_digit ( ch );
  163. }
  164. }
  165. if ( stop_idx ) {
  166. *stop_idx = i;
  167. }
  168. if ( argc < nargs ) {
  169. args [ argc++ ] = val;
  170. }
  171. return ( ( i == stub->len || ch == ':' ) && argc == nargs );
  172. }
  173. static void gdbstub_send_errno ( struct gdbstub *stub, int errno ) {
  174. gdbstub_send_num_packet ( stub, 'E', errno );
  175. }
  176. static void gdbstub_report_signal ( struct gdbstub *stub ) {
  177. gdbstub_send_num_packet ( stub, 'S', stub->signo );
  178. }
  179. static void gdbstub_read_regs ( struct gdbstub *stub ) {
  180. gdbstub_to_hex_buf ( stub->payload, ( char * ) stub->regs, GDBMACH_SIZEOF_REGS );
  181. stub->len = GDBMACH_SIZEOF_REGS * 2;
  182. gdbstub_tx_packet ( stub );
  183. }
  184. static void gdbstub_write_regs ( struct gdbstub *stub ) {
  185. if ( stub->len != 1 + GDBMACH_SIZEOF_REGS * 2 ) {
  186. gdbstub_send_errno ( stub, POSIX_EINVAL );
  187. return;
  188. }
  189. gdbstub_from_hex_buf ( ( char * ) stub->regs, &stub->payload [ 1 ], GDBMACH_SIZEOF_REGS );
  190. gdbstub_send_ok ( stub );
  191. }
  192. static void gdbstub_read_mem ( struct gdbstub *stub ) {
  193. unsigned long args [ 2 ];
  194. if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], NULL ) ) {
  195. gdbstub_send_errno ( stub, POSIX_EINVAL );
  196. return;
  197. }
  198. args [ 1 ] = ( args [ 1 ] < SIZEOF_PAYLOAD / 2 ) ? args [ 1 ] : SIZEOF_PAYLOAD / 2;
  199. gdbstub_to_hex_buf ( stub->payload, ( char * ) args [ 0 ], args [ 1 ] );
  200. stub->len = args [ 1 ] * 2;
  201. gdbstub_tx_packet ( stub );
  202. }
  203. static void gdbstub_write_mem ( struct gdbstub *stub ) {
  204. unsigned long args [ 2 ];
  205. int colon;
  206. if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], &colon ) ||
  207. colon >= stub->len || stub->payload [ colon ] != ':' ||
  208. ( stub->len - colon - 1 ) % 2 != 0 ) {
  209. gdbstub_send_errno ( stub, POSIX_EINVAL );
  210. return;
  211. }
  212. gdbstub_from_hex_buf ( ( char * ) args [ 0 ], &stub->payload [ colon + 1 ], ( stub->len - colon - 1 ) / 2 );
  213. gdbstub_send_ok ( stub );
  214. }
  215. static void gdbstub_continue ( struct gdbstub *stub, int single_step ) {
  216. gdbreg_t pc;
  217. if ( stub->len > 1 && gdbstub_get_packet_args ( stub, &pc, 1, NULL ) ) {
  218. gdbmach_set_pc ( stub->regs, pc );
  219. }
  220. gdbmach_set_single_step ( stub->regs, single_step );
  221. stub->exit_handler = 1;
  222. /* Reply will be sent when we hit the next breakpoint or interrupt */
  223. }
  224. static void gdbstub_breakpoint ( struct gdbstub *stub ) {
  225. unsigned long args [ 3 ];
  226. int enable = stub->payload [ 0 ] == 'Z' ? 1 : 0;
  227. if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], NULL ) ) {
  228. gdbstub_send_errno ( stub, POSIX_EINVAL );
  229. return;
  230. }
  231. if ( gdbmach_set_breakpoint ( args [ 0 ], args [ 1 ], args [ 2 ], enable ) ) {
  232. gdbstub_send_ok ( stub );
  233. } else {
  234. /* Not supported */
  235. stub->len = 0;
  236. gdbstub_tx_packet ( stub );
  237. }
  238. }
  239. static void gdbstub_rx_packet ( struct gdbstub *stub ) {
  240. switch ( stub->payload [ 0 ] ) {
  241. case '?':
  242. gdbstub_report_signal ( stub );
  243. break;
  244. case 'g':
  245. gdbstub_read_regs ( stub );
  246. break;
  247. case 'G':
  248. gdbstub_write_regs ( stub );
  249. break;
  250. case 'm':
  251. gdbstub_read_mem ( stub );
  252. break;
  253. case 'M':
  254. gdbstub_write_mem ( stub );
  255. break;
  256. case 'c': /* Continue */
  257. case 'k': /* Kill */
  258. case 's': /* Step */
  259. case 'D': /* Detach */
  260. gdbstub_continue ( stub, stub->payload [ 0 ] == 's' );
  261. if ( stub->payload [ 0 ] == 'D' ) {
  262. gdbstub_send_ok ( stub );
  263. }
  264. break;
  265. case 'Z': /* Insert breakpoint */
  266. case 'z': /* Remove breakpoint */
  267. gdbstub_breakpoint ( stub );
  268. break;
  269. default:
  270. stub->len = 0;
  271. gdbstub_tx_packet ( stub );
  272. break;
  273. }
  274. }
  275. /* GDB packet parser */
  276. static void gdbstub_state_new ( struct gdbstub *stub, char ch ) {
  277. if ( ch == '$' ) {
  278. stub->len = 0;
  279. stub->parse = gdbstub_state_data;
  280. }
  281. }
  282. static void gdbstub_state_data ( struct gdbstub *stub, char ch ) {
  283. if ( ch == '#' ) {
  284. stub->parse = gdbstub_state_cksum1;
  285. } else if ( ch == '$' ) {
  286. stub->len = 0; /* retry new packet */
  287. } else {
  288. /* If the length exceeds our buffer, let the checksum fail */
  289. if ( stub->len < SIZEOF_PAYLOAD ) {
  290. stub->payload [ stub->len++ ] = ch;
  291. }
  292. }
  293. }
  294. static void gdbstub_state_cksum1 ( struct gdbstub *stub, char ch ) {
  295. stub->cksum1 = gdbstub_from_hex_digit ( ch ) << 4;
  296. stub->parse = gdbstub_state_cksum2;
  297. }
  298. static void gdbstub_state_cksum2 ( struct gdbstub *stub, char ch ) {
  299. uint8_t their_cksum;
  300. uint8_t our_cksum;
  301. stub->parse = gdbstub_state_new;
  302. their_cksum = stub->cksum1 + gdbstub_from_hex_digit ( ch );
  303. our_cksum = gdbstub_cksum ( stub->payload, stub->len );
  304. if ( their_cksum == our_cksum ) {
  305. stub->trans->send ( "+", 1 );
  306. if ( stub->len > 0 ) {
  307. gdbstub_rx_packet ( stub );
  308. }
  309. } else {
  310. stub->trans->send ( "-", 1 );
  311. }
  312. }
  313. static void gdbstub_state_wait_ack ( struct gdbstub *stub, char ch ) {
  314. if ( ch == '+' ) {
  315. stub->parse = gdbstub_state_new;
  316. } else {
  317. /* This retransmit is very aggressive but necessary to keep
  318. * in sync with GDB. */
  319. gdbstub_tx_packet ( stub );
  320. }
  321. }
  322. static void gdbstub_parse ( struct gdbstub *stub, char ch ) {
  323. stub->parse ( stub, ch );
  324. }
  325. static struct gdbstub stub = {
  326. .parse = gdbstub_state_new
  327. };
  328. void gdbstub_handler ( int signo, gdbreg_t *regs ) {
  329. char packet [ SIZEOF_PAYLOAD + 4 ];
  330. size_t len, i;
  331. /* A transport must be set up */
  332. if ( !stub.trans ) {
  333. return;
  334. }
  335. stub.signo = signo;
  336. stub.regs = regs;
  337. stub.exit_handler = 0;
  338. gdbstub_report_signal ( &stub );
  339. while ( !stub.exit_handler && ( len = stub.trans->recv ( packet, sizeof ( packet ) ) ) > 0 ) {
  340. for ( i = 0; i < len; i++ ) {
  341. gdbstub_parse ( &stub, packet [ i ] );
  342. }
  343. }
  344. }
  345. struct gdb_transport *find_gdb_transport ( const char *name ) {
  346. struct gdb_transport *trans;
  347. for_each_table_entry ( trans, GDB_TRANSPORTS ) {
  348. if ( strcmp ( trans->name, name ) == 0 ) {
  349. return trans;
  350. }
  351. }
  352. return NULL;
  353. }
  354. void gdbstub_start ( struct gdb_transport *trans ) {
  355. stub.trans = trans;
  356. stub.payload = &stub.buf [ 1 ];
  357. gdbmach_breakpoint();
  358. }