Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

gdbstub.c 12KB

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