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

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