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 11KB

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