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

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