Lines Matching refs:stub

22  * GDB stub for remote debugging
46 void ( * parse ) ( struct gdbstub *stub, char ch );
58 static void gdbstub_state_new ( struct gdbstub *stub, char ch );
59 static void gdbstub_state_data ( struct gdbstub *stub, char ch );
60 static void gdbstub_state_cksum1 ( struct gdbstub *stub, char ch );
61 static void gdbstub_state_cksum2 ( struct gdbstub *stub, char ch );
62 static void gdbstub_state_wait_ack ( struct gdbstub *stub, char ch );
138 static void gdbstub_tx_packet ( struct gdbstub *stub ) {
139 uint8_t cksum = gdbstub_cksum ( stub->payload, stub->len );
140 stub->buf [ 0 ] = '$';
141 stub->buf [ stub->len + 1 ] = '#';
142 stub->buf [ stub->len + 2 ] = gdbstub_to_hex_digit ( cksum >> 4 );
143 stub->buf [ stub->len + 3 ] = gdbstub_to_hex_digit ( cksum );
144 stub->trans->send ( stub->buf, stub->len + 4 );
145 stub->parse = gdbstub_state_wait_ack;
149 static void gdbstub_send_ok ( struct gdbstub *stub ) {
150 stub->payload [ 0 ] = 'O';
151 stub->payload [ 1 ] = 'K';
152 stub->len = 2;
153 gdbstub_tx_packet ( stub );
156 static void gdbstub_send_num_packet ( struct gdbstub *stub, char reply, int num ) {
157 stub->payload [ 0 ] = reply;
158 stub->payload [ 1 ] = gdbstub_to_hex_digit ( ( char ) num >> 4 );
159 stub->payload [ 2 ] = gdbstub_to_hex_digit ( ( char ) num );
160 stub->len = 3;
161 gdbstub_tx_packet ( stub );
165 static int gdbstub_get_packet_args ( struct gdbstub *stub, unsigned long *args, int nargs, int *stop_idx ) {
170 for ( i = 1; i < stub->len && argc < nargs; i++ ) {
171 ch = stub->payload [ i ];
187 return ( ( i == stub->len || ch == ':' ) && argc == nargs );
190 static void gdbstub_send_errno ( struct gdbstub *stub, int errno ) {
191 gdbstub_send_num_packet ( stub, 'E', errno );
194 static void gdbstub_report_signal ( struct gdbstub *stub ) {
195 gdbstub_send_num_packet ( stub, 'S', stub->signo );
198 static void gdbstub_read_regs ( struct gdbstub *stub ) {
199 gdbstub_to_hex_buf ( stub->payload, ( char * ) stub->regs, GDBMACH_SIZEOF_REGS );
200 stub->len = GDBMACH_SIZEOF_REGS * 2;
201 gdbstub_tx_packet ( stub );
204 static void gdbstub_write_regs ( struct gdbstub *stub ) {
205 if ( stub->len != 1 + GDBMACH_SIZEOF_REGS * 2 ) {
206 gdbstub_send_errno ( stub, POSIX_EINVAL );
209 gdbstub_from_hex_buf ( ( char * ) stub->regs, &stub->payload [ 1 ], GDBMACH_SIZEOF_REGS );
210 gdbstub_send_ok ( stub );
213 static void gdbstub_read_mem ( struct gdbstub *stub ) {
215 if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], NULL ) ) {
216 gdbstub_send_errno ( stub, POSIX_EINVAL );
220 gdbstub_to_hex_buf ( stub->payload, ( char * ) args [ 0 ], args [ 1 ] );
221 stub->len = args [ 1 ] * 2;
222 gdbstub_tx_packet ( stub );
225 static void gdbstub_write_mem ( struct gdbstub *stub ) {
228 if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], &colon ) ||
229 colon >= stub->len || stub->payload [ colon ] != ':' ||
230 ( stub->len - colon - 1 ) % 2 != 0 ) {
231 gdbstub_send_errno ( stub, POSIX_EINVAL );
234 gdbstub_from_hex_buf ( ( char * ) args [ 0 ], &stub->payload [ colon + 1 ], ( stub->len - colon - 1 ) / 2 );
235 gdbstub_send_ok ( stub );
238 static void gdbstub_continue ( struct gdbstub *stub, int single_step ) {
240 if ( stub->len > 1 && gdbstub_get_packet_args ( stub, &pc, 1, NULL ) ) {
241 gdbmach_set_pc ( stub->regs, pc );
243 gdbmach_set_single_step ( stub->regs, single_step );
244 stub->exit_handler = 1;
248 static void gdbstub_breakpoint ( struct gdbstub *stub ) {
250 int enable = stub->payload [ 0 ] == 'Z' ? 1 : 0;
251 if ( !gdbstub_get_packet_args ( stub, args, sizeof args / sizeof args [ 0 ], NULL ) ) {
252 gdbstub_send_errno ( stub, POSIX_EINVAL );
256 gdbstub_send_ok ( stub );
259 stub->len = 0;
260 gdbstub_tx_packet ( stub );
264 static void gdbstub_rx_packet ( struct gdbstub *stub ) {
265 switch ( stub->payload [ 0 ] ) {
267 gdbstub_report_signal ( stub );
270 gdbstub_read_regs ( stub );
273 gdbstub_write_regs ( stub );
276 gdbstub_read_mem ( stub );
279 gdbstub_write_mem ( stub );
285 gdbstub_continue ( stub, stub->payload [ 0 ] == 's' );
286 if ( stub->payload [ 0 ] == 'D' ) {
287 gdbstub_send_ok ( stub );
292 gdbstub_breakpoint ( stub );
295 stub->len = 0;
296 gdbstub_tx_packet ( stub );
302 static void gdbstub_state_new ( struct gdbstub *stub, char ch ) {
304 stub->len = 0;
305 stub->parse = gdbstub_state_data;
309 static void gdbstub_state_data ( struct gdbstub *stub, char ch ) {
311 stub->parse = gdbstub_state_cksum1;
313 stub->len = 0; /* retry new packet */
316 if ( stub->len < SIZEOF_PAYLOAD ) {
317 stub->payload [ stub->len++ ] = ch;
322 static void gdbstub_state_cksum1 ( struct gdbstub *stub, char ch ) {
323 stub->cksum1 = gdbstub_from_hex_digit ( ch ) << 4;
324 stub->parse = gdbstub_state_cksum2;
327 static void gdbstub_state_cksum2 ( struct gdbstub *stub, char ch ) {
331 stub->parse = gdbstub_state_new;
332 their_cksum = stub->cksum1 + gdbstub_from_hex_digit ( ch );
333 our_cksum = gdbstub_cksum ( stub->payload, stub->len );
335 stub->trans->send ( "+", 1 );
336 if ( stub->len > 0 ) {
337 gdbstub_rx_packet ( stub );
340 stub->trans->send ( "-", 1 );
344 static void gdbstub_state_wait_ack ( struct gdbstub *stub, char ch ) {
346 stub->parse = gdbstub_state_new;
350 gdbstub_tx_packet ( stub );
354 static void gdbstub_parse ( struct gdbstub *stub, char ch ) {
355 stub->parse ( stub, ch );
358 static struct gdbstub stub = {
367 if ( !stub.trans ) {
371 stub.signo = signo;
372 stub.regs = regs;
373 stub.exit_handler = 0;
374 gdbstub_report_signal ( &stub );
375 while ( !stub.exit_handler && ( len = stub.trans->recv ( packet, sizeof ( packet ) ) ) > 0 ) {
377 gdbstub_parse ( &stub, packet [ i ] );
394 stub.trans = trans;
395 stub.payload = &stub.buf [ 1 ];