00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <ctype.h>
00030 #include <byteswap.h>
00031 #include <gpxe/gdbstub.h>
00032 #include "gdbmach.h"
00033
00034 enum {
00035 POSIX_EINVAL = 0x1c,
00036 SIZEOF_PAYLOAD = 256,
00037 };
00038
00039 struct gdbstub {
00040 struct gdb_transport *trans;
00041 int exit_handler;
00042
00043 int signo;
00044 gdbreg_t *regs;
00045
00046 void ( * parse ) ( struct gdbstub *stub, char ch );
00047 uint8_t cksum1;
00048
00049
00050
00051
00052 char buf [ SIZEOF_PAYLOAD + 4 ];
00053 char *payload;
00054 int len;
00055 };
00056
00057
00058 static void gdbstub_state_new ( struct gdbstub *stub, char ch );
00059 static void gdbstub_state_data ( struct gdbstub *stub, char ch );
00060 static void gdbstub_state_cksum1 ( struct gdbstub *stub, char ch );
00061 static void gdbstub_state_cksum2 ( struct gdbstub *stub, char ch );
00062 static void gdbstub_state_wait_ack ( struct gdbstub *stub, char ch );
00063
00064 static uint8_t gdbstub_from_hex_digit ( char ch ) {
00065 return ( isdigit ( ch ) ? ch - '0' : tolower ( ch ) - 'a' + 0xa ) & 0xf;
00066 }
00067
00068 static uint8_t gdbstub_to_hex_digit ( uint8_t b ) {
00069 b &= 0xf;
00070 return ( b < 0xa ? '0' : 'a' - 0xa ) + b;
00071 }
00072
00073
00074
00075
00076
00077
00078 static void gdbstub_from_hex_buf ( char *dst, char *src, int lenbytes ) {
00079 if ( lenbytes == 2 && ( ( unsigned long ) dst & 0x1 ) == 0 ) {
00080 uint16_t i = gdbstub_from_hex_digit ( src [ 2 ] ) << 12 |
00081 gdbstub_from_hex_digit ( src [ 3 ] ) << 8 |
00082 gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
00083 gdbstub_from_hex_digit ( src [ 1 ] );
00084 * ( uint16_t * ) dst = cpu_to_le16 ( i );
00085 } else if ( lenbytes == 4 && ( ( unsigned long ) dst & 0x3 ) == 0 ) {
00086 uint32_t i = gdbstub_from_hex_digit ( src [ 6 ] ) << 28 |
00087 gdbstub_from_hex_digit ( src [ 7 ] ) << 24 |
00088 gdbstub_from_hex_digit ( src [ 4 ] ) << 20 |
00089 gdbstub_from_hex_digit ( src [ 5 ] ) << 16 |
00090 gdbstub_from_hex_digit ( src [ 2 ] ) << 12 |
00091 gdbstub_from_hex_digit ( src [ 3 ] ) << 8 |
00092 gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
00093 gdbstub_from_hex_digit ( src [ 1 ] );
00094 * ( uint32_t * ) dst = cpu_to_le32 ( i );
00095 } else {
00096 while ( lenbytes-- > 0 ) {
00097 *dst++ = gdbstub_from_hex_digit ( src [ 0 ] ) << 4 |
00098 gdbstub_from_hex_digit ( src [ 1 ] );
00099 src += 2;
00100 }
00101 }
00102 }
00103
00104 static void gdbstub_to_hex_buf ( char *dst, char *src, int lenbytes ) {
00105 if ( lenbytes == 2 && ( ( unsigned long ) src & 0x1 ) == 0 ) {
00106 uint16_t i = cpu_to_le16 ( * ( uint16_t * ) src );
00107 dst [ 0 ] = gdbstub_to_hex_digit ( i >> 4 );
00108 dst [ 1 ] = gdbstub_to_hex_digit ( i );
00109 dst [ 2 ] = gdbstub_to_hex_digit ( i >> 12 );
00110 dst [ 3 ] = gdbstub_to_hex_digit ( i >> 8 );
00111 } else if ( lenbytes == 4 && ( ( unsigned long ) src & 0x3 ) == 0 ) {
00112 uint32_t i = cpu_to_le32 ( * ( uint32_t * ) src );
00113 dst [ 0 ] = gdbstub_to_hex_digit ( i >> 4 );
00114 dst [ 1 ] = gdbstub_to_hex_digit ( i );
00115 dst [ 2 ] = gdbstub_to_hex_digit ( i >> 12 );
00116 dst [ 3 ] = gdbstub_to_hex_digit ( i >> 8 );
00117 dst [ 4 ] = gdbstub_to_hex_digit ( i >> 20 );
00118 dst [ 5 ] = gdbstub_to_hex_digit ( i >> 16);
00119 dst [ 6 ] = gdbstub_to_hex_digit ( i >> 28 );
00120 dst [ 7 ] = gdbstub_to_hex_digit ( i >> 24 );
00121 } else {
00122 while ( lenbytes-- > 0 ) {
00123 *dst++ = gdbstub_to_hex_digit ( *src >> 4 );
00124 *dst++ = gdbstub_to_hex_digit ( *src );
00125 src++;
00126 }
00127 }
00128 }
00129
00130 static uint8_t gdbstub_cksum ( char *data, int len ) {
00131 uint8_t cksum = 0;
00132 while ( len-- > 0 ) {
00133 cksum += ( uint8_t ) *data++;
00134 }
00135 return cksum;
00136 }
00137
00138 static void gdbstub_tx_packet ( struct gdbstub *stub ) {
00139 uint8_t cksum = gdbstub_cksum ( stub->payload, stub->len );
00140 stub->buf [ 0 ] = '$';
00141 stub->buf [ stub->len + 1 ] = '#';
00142 stub->buf [ stub->len + 2 ] = gdbstub_to_hex_digit ( cksum >> 4 );
00143 stub->buf [ stub->len + 3 ] = gdbstub_to_hex_digit ( cksum );
00144 stub->trans->send ( stub->buf, stub->len + 4 );
00145 stub->parse = gdbstub_state_wait_ack;
00146 }
00147
00148
00149 static void gdbstub_send_ok ( struct gdbstub *stub ) {
00150 stub->payload [ 0 ] = 'O';
00151 stub->payload [ 1 ] = 'K';
00152 stub->len = 2;
00153 gdbstub_tx_packet ( stub );
00154 }
00155
00156 static void gdbstub_send_num_packet ( struct gdbstub *stub, char reply, int num ) {
00157 stub->payload [ 0 ] = reply;
00158 stub->payload [ 1 ] = gdbstub_to_hex_digit ( ( char ) num >> 4 );
00159 stub->payload [ 2 ] = gdbstub_to_hex_digit ( ( char ) num );
00160 stub->len = 3;
00161 gdbstub_tx_packet ( stub );
00162 }
00163
00164
00165 static int gdbstub_get_packet_args ( struct gdbstub *stub, unsigned long *args, int nargs, int *stop_idx ) {
00166 int i;
00167 char ch = 0;
00168 int argc = 0;
00169 unsigned long val = 0;
00170 for ( i = 1; i < stub->len && argc < nargs; i++ ) {
00171 ch = stub->payload [ i ];
00172 if ( ch == ':' ) {
00173 break;
00174 } else if ( ch == ',' ) {
00175 args [ argc++ ] = val;
00176 val = 0;
00177 } else {
00178 val = ( val << 4 ) | gdbstub_from_hex_digit ( ch );
00179 }
00180 }
00181 if ( stop_idx ) {
00182 *stop_idx = i;
00183 }
00184 if ( argc < nargs ) {
00185 args [ argc++ ] = val;
00186 }
00187 return ( ( i == stub->len || ch == ':' ) && argc == nargs );
00188 }
00189
00190 static void gdbstub_send_errno ( struct gdbstub *stub, int errno ) {
00191 gdbstub_send_num_packet ( stub, 'E', errno );
00192 }
00193
00194 static void gdbstub_report_signal ( struct gdbstub *stub ) {
00195 gdbstub_send_num_packet ( stub, 'S', stub->signo );
00196 }
00197
00198 static void gdbstub_read_regs ( struct gdbstub *stub ) {
00199 gdbstub_to_hex_buf ( stub->payload, ( char * ) stub->regs, GDBMACH_SIZEOF_REGS );
00200 stub->len = GDBMACH_SIZEOF_REGS * 2;
00201 gdbstub_tx_packet ( stub );
00202 }
00203
00204 static void gdbstub_write_regs ( struct gdbstub *stub ) {
00205 if ( stub->len != 1 + GDBMACH_SIZEOF_REGS * 2 ) {
00206 gdbstub_send_errno ( stub, POSIX_EINVAL );
00207 return;
00208 }
00209 gdbstub_from_hex_buf ( ( char * ) stub->regs, &stub->payload [ 1 ], GDBMACH_SIZEOF_REGS );
00210 gdbstub_send_ok ( stub );
00211 }
00212
00213 static void gdbstub_read_mem ( struct gdbstub *stub ) {
00214 unsigned long args [ 2 ];
00215 if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], NULL ) ) {
00216 gdbstub_send_errno ( stub, POSIX_EINVAL );
00217 return;
00218 }
00219 args [ 1 ] = ( args [ 1 ] < SIZEOF_PAYLOAD / 2 ) ? args [ 1 ] : SIZEOF_PAYLOAD / 2;
00220 gdbstub_to_hex_buf ( stub->payload, ( char * ) args [ 0 ], args [ 1 ] );
00221 stub->len = args [ 1 ] * 2;
00222 gdbstub_tx_packet ( stub );
00223 }
00224
00225 static void gdbstub_write_mem ( struct gdbstub *stub ) {
00226 unsigned long args [ 2 ];
00227 int colon;
00228 if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], &colon ) ||
00229 colon >= stub->len || stub->payload [ colon ] != ':' ||
00230 ( stub->len - colon - 1 ) % 2 != 0 ) {
00231 gdbstub_send_errno ( stub, POSIX_EINVAL );
00232 return;
00233 }
00234 gdbstub_from_hex_buf ( ( char * ) args [ 0 ], &stub->payload [ colon + 1 ], ( stub->len - colon - 1 ) / 2 );
00235 gdbstub_send_ok ( stub );
00236 }
00237
00238 static void gdbstub_continue ( struct gdbstub *stub, int single_step ) {
00239 gdbreg_t pc;
00240 if ( stub->len > 1 && gdbstub_get_packet_args ( stub, &pc, 1, NULL ) ) {
00241 gdbmach_set_pc ( stub->regs, pc );
00242 }
00243 gdbmach_set_single_step ( stub->regs, single_step );
00244 stub->exit_handler = 1;
00245
00246 }
00247
00248 static void gdbstub_breakpoint ( struct gdbstub *stub ) {
00249 unsigned long args [ 3 ];
00250 int enable = stub->payload [ 0 ] == 'Z' ? 1 : 0;
00251 if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], NULL ) ) {
00252 gdbstub_send_errno ( stub, POSIX_EINVAL );
00253 return;
00254 }
00255 if ( gdbmach_set_breakpoint ( args [ 0 ], args [ 1 ], args [ 2 ], enable ) ) {
00256 gdbstub_send_ok ( stub );
00257 } else {
00258
00259 stub->len = 0;
00260 gdbstub_tx_packet ( stub );
00261 }
00262 }
00263
00264 static void gdbstub_rx_packet ( struct gdbstub *stub ) {
00265 switch ( stub->payload [ 0 ] ) {
00266 case '?':
00267 gdbstub_report_signal ( stub );
00268 break;
00269 case 'g':
00270 gdbstub_read_regs ( stub );
00271 break;
00272 case 'G':
00273 gdbstub_write_regs ( stub );
00274 break;
00275 case 'm':
00276 gdbstub_read_mem ( stub );
00277 break;
00278 case 'M':
00279 gdbstub_write_mem ( stub );
00280 break;
00281 case 'c':
00282 case 'k':
00283 case 's':
00284 case 'D':
00285 gdbstub_continue ( stub, stub->payload [ 0 ] == 's' );
00286 if ( stub->payload [ 0 ] == 'D' ) {
00287 gdbstub_send_ok ( stub );
00288 }
00289 break;
00290 case 'Z':
00291 case 'z':
00292 gdbstub_breakpoint ( stub );
00293 break;
00294 default:
00295 stub->len = 0;
00296 gdbstub_tx_packet ( stub );
00297 break;
00298 }
00299 }
00300
00301
00302 static void gdbstub_state_new ( struct gdbstub *stub, char ch ) {
00303 if ( ch == '$' ) {
00304 stub->len = 0;
00305 stub->parse = gdbstub_state_data;
00306 }
00307 }
00308
00309 static void gdbstub_state_data ( struct gdbstub *stub, char ch ) {
00310 if ( ch == '#' ) {
00311 stub->parse = gdbstub_state_cksum1;
00312 } else if ( ch == '$' ) {
00313 stub->len = 0;
00314 } else {
00315
00316 if ( stub->len < SIZEOF_PAYLOAD ) {
00317 stub->payload [ stub->len++ ] = ch;
00318 }
00319 }
00320 }
00321
00322 static void gdbstub_state_cksum1 ( struct gdbstub *stub, char ch ) {
00323 stub->cksum1 = gdbstub_from_hex_digit ( ch ) << 4;
00324 stub->parse = gdbstub_state_cksum2;
00325 }
00326
00327 static void gdbstub_state_cksum2 ( struct gdbstub *stub, char ch ) {
00328 uint8_t their_cksum;
00329 uint8_t our_cksum;
00330
00331 stub->parse = gdbstub_state_new;
00332 their_cksum = stub->cksum1 + gdbstub_from_hex_digit ( ch );
00333 our_cksum = gdbstub_cksum ( stub->payload, stub->len );
00334 if ( their_cksum == our_cksum ) {
00335 stub->trans->send ( "+", 1 );
00336 if ( stub->len > 0 ) {
00337 gdbstub_rx_packet ( stub );
00338 }
00339 } else {
00340 stub->trans->send ( "-", 1 );
00341 }
00342 }
00343
00344 static void gdbstub_state_wait_ack ( struct gdbstub *stub, char ch ) {
00345 if ( ch == '+' ) {
00346 stub->parse = gdbstub_state_new;
00347 } else {
00348
00349
00350 gdbstub_tx_packet ( stub );
00351 }
00352 }
00353
00354 static void gdbstub_parse ( struct gdbstub *stub, char ch ) {
00355 stub->parse ( stub, ch );
00356 }
00357
00358 static struct gdbstub stub = {
00359 .parse = gdbstub_state_new
00360 };
00361
00362 void gdbstub_handler ( int signo, gdbreg_t *regs ) {
00363 char packet [ SIZEOF_PAYLOAD + 4 ];
00364 size_t len, i;
00365
00366
00367 if ( !stub.trans ) {
00368 return;
00369 }
00370
00371 stub.signo = signo;
00372 stub.regs = regs;
00373 stub.exit_handler = 0;
00374 gdbstub_report_signal ( &stub );
00375 while ( !stub.exit_handler && ( len = stub.trans->recv ( packet, sizeof ( packet ) ) ) > 0 ) {
00376 for ( i = 0; i < len; i++ ) {
00377 gdbstub_parse ( &stub, packet [ i ] );
00378 }
00379 }
00380 }
00381
00382 struct gdb_transport *find_gdb_transport ( const char *name ) {
00383 struct gdb_transport *trans;
00384
00385 for_each_table_entry ( trans, GDB_TRANSPORTS ) {
00386 if ( strcmp ( trans->name, name ) == 0 ) {
00387 return trans;
00388 }
00389 }
00390 return NULL;
00391 }
00392
00393 void gdbstub_start ( struct gdb_transport *trans ) {
00394 stub.trans = trans;
00395 stub.payload = &stub.buf [ 1 ];
00396 gdbmach_breakpoint();
00397 }